Change in openvpn[master]: dns: do not use netsh to set name server addresses
A message that is not in the archive (sent off-list or not yet received): gerrit.1733328040000.Id2bed0908e84c19b8fb6fe806376316793e550b4@...2715...
Attention is currently required from: flichtenheld, plaisthos.
Hello plaisthos, flichtenheld,
I'd like you to do a code review.
Please visit
http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
to review the following change.
Change subject: dns: do not use netsh to set name server addresses
......................................................................
dns: do not use netsh to set name server addresses
Instead of spawning a netsh process, set the name server addresses directly in the registry hive of the VPN interface.
This is a first step to get rid of the use of command line tools in the service and move to a more API driven style of modifying the VPN adapter configuration.
---
M src/openvpnserv/interactive.c
1 file changed, 41 insertions(+), 115 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/25/825/1
src/openvpnserv/interactive.c +41 -115
@@ -1024,65 +1024,6 @@ }/** -* Run the command: netsh interface $proto $action dns $if_name $addr [validate=no] -* @param action "delete" or "add" -* @param proto "ipv6" or "ip" -* @param if_name "name_of_interface" -* @param addr IPv4 (for proto = ip) or IPv6 address as a string -* -* If addr is null and action = "delete" all addresses are deleted. -*/ -static DWORD -netsh_dns_cmd (const wchar_t *action,const wchar_t *proto,const wchar_t *if_name,const wchar_t *addr) -{ -DWORD err =0 ; -int timeout =30000 ;/* in msec */ -wchar_t argv0[MAX_PATH ]; -wchar_t *cmdline =NULL ; - -if (!addr) - { -if (wcscmp (action,L"delete" ) ==0 ) - { - addr =L"all" ; - } -else /* nothing to do -- return success*/ - { - goto out; - } - } - -/* Path of netsh */ -swprintf (argv0,_countof (argv0),L"%ls\\%ls" ,get_win_sys_path (),L"netsh.exe" ); - -/* cmd template: -* netsh interface $proto $action dns $if_name $addr [validate=no] -*/ -const wchar_t *fmt =L"netsh interface %ls %ls dns \"%ls\" %ls" ; - -/* max cmdline length in wchars -- include room for worst case and some */ -size_t ncmdline =wcslen (fmt) +wcslen (if_name) +wcslen (addr) +32 +1 ; - cmdline =malloc (ncmdline*sizeof (wchar_t)); -if (!cmdline) - { - err =ERROR_OUTOFMEMORY ; - goto out; - } - -swprintf (cmdline, ncmdline, fmt, proto, action, if_name, addr); - -if (IsWindows7OrGreater ()) - { -wcscat_s (cmdline, ncmdline,L" validate=no" ); - } - err =ExecCommand (argv0, cmdline, timeout); - -out: -free (cmdline); -return err; -} - -/** * Run the command: netsh interface ip $action wins $if_name [static] $addr * @param action "delete", "add" or "set" * @param if_name "name_of_interface"
@@ -1139,22 +1080,6 @@return err; } -/* Delete all IPv4 or IPv6 dns servers for an interface */ -static DWORD -DeleteDNS (short family,wchar_t *if_name) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"delete" , proto, if_name,NULL ); -} - -/* Add an IPv4 or IPv6 dns server to an interface */ -static DWORD -AddDNS (short family,wchar_t *if_name,wchar_t *addr) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"add" , proto, if_name, addr); -} -static BOOL CmpWString (LPVOID item,LPVOID str) {
@@ -1810,7 +1735,6 @@ HandleDNSConfigMessage(const dns_cfg_message_t *msg, undo_lists_t *lists) { DWORD err = 0; -wchar_t addr[46 ];/* large enough to hold string representation of an ipv4 / ipv6 address */ undo_type_t undo_type = (msg->family ==AF_INET6 ) ? undo_dns4 : undo_dns6;int addr_len = msg->addr_len;
@@ -1832,10 +1756,11 @@ msgptr->domains[_countof (msg->domains)-1 ] ='\0' ; } -wchar_t *wide_name =utf8to16 (msg->iface.name);/* utf8 to wide-char */ -if (!wide_name) +WCHAR iid[64 ]; + err =InterfaceIdString (msg->iface.name, iid,_countof (iid)); +if (err) { -return ERROR_OUTOFMEMORY ; +return err; }/* We delete all current addresses before adding any
@@ -1843,12 +1768,12 @@*/ if (addr_len >0 || msg->header.type == msg_del_dns_cfg) { - err =DeleteDNS (msg->family, wide_name); + err =ResetNameServers (iid, msg->family);if (err) { - goto out; +return err; } -free (RemoveListItem (&(*lists)[undo_type], CmpWString, wide_name)); +free (RemoveListItem (&(*lists)[undo_type], CmpAny, iid)); }if (msg->header.type == msg_del_dns_cfg)
@@ -1860,40 +1785,43 @@ err =SetDnsSearchDomains (msg->iface.name,NULL , &gpol, lists); }ApplyDnsSettings (gpol); - goto out;/* job done */ +return err;/* job done */ } -for (int i =0 ; i < addr_len; ++i) - { -if (msg->family ==AF_INET6 ) - { -RtlIpv6AddressToStringW (&msg->addr[i].ipv6, addr); - } -else - { -RtlIpv4AddressToStringW (&msg->addr[i].ipv4, addr); - } - err =AddDNS (msg->family, wide_name, addr); -if (i ==0 && err) - { - goto out; - } -/* We do not check for duplicate addresses, so any error in adding -* additional addresses is ignored. -*/ - } - - err =0 ; -if (msg->addr_len >0 ) { -wchar_t *tmp_name =_wcsdup (wide_name); -if (!tmp_name ||AddListItem (&(*lists)[undo_type], tmp_name)) +/* prepare the comma separated address list */ +CHAR addrs[256 ];/* large enough to hold four IPv4 / IPv6 address strings */ +size_t offset =0 ; +for (int i =0 ; i < addr_len; ++i) { -free (tmp_name); -DeleteDNS (msg->family, wide_name); - err =ERROR_OUTOFMEMORY ; - goto out; +if (i !=0 ) + { + addrs[offset++] =',' ; + } +if (msg->family ==AF_INET6 ) + { +RtlIpv6AddressToStringA (&msg->addr[i].ipv6, addrs + offset); + } +else + { +RtlIpv4AddressToStringA (&msg->addr[i].ipv4, addrs + offset); + } + offset +=strlen (addrs); + } + + err =SetNameServers (iid, msg->family, addrs); +if (err) + { +return err; + } + +wchar_t *tmp_iid =_wcsdup (iid); +if (!tmp_iid ||AddListItem (&(*lists)[undo_type], tmp_iid)) + { +free (tmp_iid); +ResetNameServers (iid, msg->family); +return ERROR_OUTOFMEMORY ; } }
@@ -1904,8 +1832,6 @@ }ApplyDnsSettings (gpol); -out: -free (wide_name);return err; }
@@ -2286,11 +2212,11 @@break ;case undo_dns4: -DeleteDNS (AF_INET , item->data); +ResetNameServers (item->data,AF_INET );break ;case undo_dns6: -DeleteDNS (AF_INET6 , item->data); +ResetNameServers (item->data,AF_INET6 );break ;break ;
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id2bed0908e84c19b8fb6fe806376316793e550b4
Gerrit-Change-Number: 825
Gerrit-PatchSet: 1
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-MessageType: newchange
Attention is currently required from: flichtenheld, plaisthos.
Hello flichtenheld, plaisthos,
I'd like you to reexamine a change. Please visit
http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
to look at the new patch set (#2).
Change subject: dns: do not use netsh to set name server addresses
......................................................................
dns: do not use netsh to set name server addresses
Instead of spawning a netsh process, set the name server addresses directly in the registry hive of the VPN interface.
This is a first step to get rid of the use of command line tools in the service and move to a more API driven style of modifying the VPN adapter configuration.
---
M src/openvpnserv/interactive.c
1 file changed, 146 insertions(+), 115 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/25/825/2
src/openvpnserv/interactive.c +146 -115
@@ -1024,65 +1024,6 @@ }/** -* Run the command: netsh interface $proto $action dns $if_name $addr [validate=no] -* @param action "delete" or "add" -* @param proto "ipv6" or "ip" -* @param if_name "name_of_interface" -* @param addr IPv4 (for proto = ip) or IPv6 address as a string -* -* If addr is null and action = "delete" all addresses are deleted. -*/ -static DWORD -netsh_dns_cmd (const wchar_t *action,const wchar_t *proto,const wchar_t *if_name,const wchar_t *addr) -{ -DWORD err =0 ; -int timeout =30000 ;/* in msec */ -wchar_t argv0[MAX_PATH ]; -wchar_t *cmdline =NULL ; - -if (!addr) - { -if (wcscmp (action,L"delete" ) ==0 ) - { - addr =L"all" ; - } -else /* nothing to do -- return success*/ - { - goto out; - } - } - -/* Path of netsh */ -swprintf (argv0,_countof (argv0),L"%ls\\%ls" ,get_win_sys_path (),L"netsh.exe" ); - -/* cmd template: -* netsh interface $proto $action dns $if_name $addr [validate=no] -*/ -const wchar_t *fmt =L"netsh interface %ls %ls dns \"%ls\" %ls" ; - -/* max cmdline length in wchars -- include room for worst case and some */ -size_t ncmdline =wcslen (fmt) +wcslen (if_name) +wcslen (addr) +32 +1 ; - cmdline =malloc (ncmdline*sizeof (wchar_t)); -if (!cmdline) - { - err =ERROR_OUTOFMEMORY ; - goto out; - } - -swprintf (cmdline, ncmdline, fmt, proto, action, if_name, addr); - -if (IsWindows7OrGreater ()) - { -wcscat_s (cmdline, ncmdline,L" validate=no" ); - } - err =ExecCommand (argv0, cmdline, timeout); - -out: -free (cmdline); -return err; -} - -/** * Run the command: netsh interface ip $action wins $if_name [static] $addr * @param action "delete", "add" or "set" * @param if_name "name_of_interface"
@@ -1139,22 +1080,6 @@return err; } -/* Delete all IPv4 or IPv6 dns servers for an interface */ -static DWORD -DeleteDNS (short family,wchar_t *if_name) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"delete" , proto, if_name,NULL ); -} - -/* Add an IPv4 or IPv6 dns server to an interface */ -static DWORD -AddDNS (short family,wchar_t *if_name,wchar_t *addr) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"add" , proto, if_name, addr); -} -static BOOL CmpWString (LPVOID item,LPVOID str) {
@@ -1806,11 +1731,115 @@return err; } +/** +* Return the interfaces registry key for the specified address family +* +* @param family the internet address family to open the key for +* @param key PHKEY to return the key in +* @return BOOL to indicate success or failure +*/ +static BOOL +GetInterfacesKey (short family,PHKEY key) +{ +PCSTR itfs_key = family ==AF_INET6 + ?"SYSTEM\\CurrentControlSet\\Services\\Tcpip6\\Parameters\\Interfaces" + :"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces" ; + +LSTATUS err =RegOpenKeyExA (HKEY_LOCAL_MACHINE , itfs_key,0 ,KEY_ALL_ACCESS , key); +if (err) + { + *key =INVALID_HANDLE_VALUE ; +MsgToEventLog (M_SYSERR ,TEXT ("GetInterfacesKey: " +"could not open interfaces registry key for family %d (%lu)" ), + family, err); + } + +return err ? FALSE : TRUE; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param value the value to set the name servers to +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServersValue (PCWSTR itf_id,short family,PCSTR value) +{ +DWORD err; + +HKEY itfs; +if (!GetInterfacesKey (family, &itfs)) + { +return ERROR_FILE_NOT_FOUND ; + } + +HKEY itf =INVALID_HANDLE_VALUE ; + err =RegOpenKeyExW (itfs, itf_id,0 ,KEY_ALL_ACCESS , &itf); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not open interface key for %s family %d (%lu)" ), + itf_id, family, err); + goto out; + } + + err =RegSetValueExA (itf,"NameServer" ,0 ,REG_SZ , (PBYTE )value,strlen (value) +1 ); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not set name servers '%S' for %s family %d (%lu)" ), + value, itf_id, family, err); + } + +out: +if (itf !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itf); + } +if (itfs !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itfs); + } +return err; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param addrs comma separated list of name server addresses +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServers (PCWSTR itf_id,short family,PCSTR addrs) +{ +return SetNameServersValue (itf_id, family, addrs); +} + +/** +* Delete all DNS name servers from a registry interface configuration +* +* @param itf_id the interface id to clear the servers for +* @param family internet address family to clear the servers for +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +ResetNameServers (PCWSTR itf_id,short family) +{ +return SetNameServersValue (itf_id, family,"" ); +} +static DWORD HandleDNSConfigMessage (const dns_cfg_message_t *msg,undo_lists_t *lists) {DWORD err =0 ; -wchar_t addr[46 ];/* large enough to hold string representation of an ipv4 / ipv6 address */ undo_type_t undo_type = (msg->family ==AF_INET6 ) ? undo_dns4 : undo_dns6;int addr_len = msg->addr_len;
@@ -1832,10 +1861,11 @@ msgptr->domains[_countof (msg->domains)-1 ] ='\0' ; } -wchar_t *wide_name =utf8to16 (msg->iface.name);/* utf8 to wide-char */ -if (!wide_name) +WCHAR iid[64 ]; + err =InterfaceIdString (msg->iface.name, iid,_countof (iid)); +if (err) { -return ERROR_OUTOFMEMORY ; +return err; }/* We delete all current addresses before adding any
@@ -1843,12 +1873,12 @@*/ if (addr_len >0 || msg->header.type == msg_del_dns_cfg) { - err =DeleteDNS (msg->family, wide_name); + err =ResetNameServers (iid, msg->family);if (err) { - goto out; +return err; } -free (RemoveListItem (&(*lists)[undo_type], CmpWString, wide_name)); +free (RemoveListItem (&(*lists)[undo_type], CmpAny, iid)); }if (msg->header.type == msg_del_dns_cfg)
@@ -1860,40 +1890,43 @@ err =SetDnsSearchDomains (msg->iface.name,NULL , &gpol, lists); }ApplyDnsSettings (gpol); - goto out;/* job done */ +return err;/* job done */ } -for (int i =0 ; i < addr_len; ++i) - { -if (msg->family ==AF_INET6 ) - { -RtlIpv6AddressToStringW (&msg->addr[i].ipv6, addr); - } -else - { -RtlIpv4AddressToStringW (&msg->addr[i].ipv4, addr); - } - err =AddDNS (msg->family, wide_name, addr); -if (i ==0 && err) - { - goto out; - } -/* We do not check for duplicate addresses, so any error in adding -* additional addresses is ignored. -*/ - } - - err =0 ; -if (msg->addr_len >0 ) { -wchar_t *tmp_name =_wcsdup (wide_name); -if (!tmp_name ||AddListItem (&(*lists)[undo_type], tmp_name)) +/* prepare the comma separated address list */ +CHAR addrs[256 ];/* large enough to hold four IPv4 / IPv6 address strings */ +size_t offset =0 ; +for (int i =0 ; i < addr_len; ++i) { -free (tmp_name); -DeleteDNS (msg->family, wide_name); - err =ERROR_OUTOFMEMORY ; - goto out; +if (i !=0 ) + { + addrs[offset++] =',' ; + } +if (msg->family ==AF_INET6 ) + { +RtlIpv6AddressToStringA (&msg->addr[i].ipv6, addrs + offset); + } +else + { +RtlIpv4AddressToStringA (&msg->addr[i].ipv4, addrs + offset); + } + offset +=strlen (addrs); + } + + err =SetNameServers (iid, msg->family, addrs); +if (err) + { +return err; + } + +wchar_t *tmp_iid =_wcsdup (iid); +if (!tmp_iid ||AddListItem (&(*lists)[undo_type], tmp_iid)) + { +free (tmp_iid); +ResetNameServers (iid, msg->family); +return ERROR_OUTOFMEMORY ; } }
@@ -1904,8 +1937,6 @@ }ApplyDnsSettings (gpol); -out: -free (wide_name);return err; }
@@ -2286,11 +2317,11 @@break ;case undo_dns4: -DeleteDNS (AF_INET , item->data); +ResetNameServers (item->data,AF_INET );break ;case undo_dns6: -DeleteDNS (AF_INET6 , item->data); +ResetNameServers (item->data,AF_INET6 );break ;break ;
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id2bed0908e84c19b8fb6fe806376316793e550b4
Gerrit-Change-Number: 825
Gerrit-PatchSet: 2
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-MessageType: newpatchset
Attention is currently required from: d12fk, flichtenheld, plaisthos.
stipa has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/825?usp=email )
Change subject: dns: do not use netsh to set name server addresses
......................................................................
Patch Set 2:
(1 comment)
File src/openvpnserv/interactive.c:
http://gerrit.openvpn.net/c/openvpn/+/825/comment/7e06254d_29fcb9b6 :
PS2, Line 1748: LSTATUS err = RegOpenKeyExA(HKEY_LOCAL_MACHINE, itfs_key, 0, KEY_ALL_ACCESS, key);
Here we use RegOpenKeyExA, and in the function below RegOpenKeyExW
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id2bed0908e84c19b8fb6fe806376316793e550b4
Gerrit-Change-Number: 825
Gerrit-PatchSet: 2
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-CC: stipa <[email protected]...>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Comment-Date: Wed, 04 Dec 2024 17:56:22 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment
Attention is currently required from: flichtenheld, plaisthos, stipa.
d12fk has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/825?usp=email )
Change subject: dns: do not use netsh to set name server addresses
......................................................................
Patch Set 2:
(1 comment)
File src/openvpnserv/interactive.c:
http://gerrit.openvpn.net/c/openvpn/+/825/comment/e62ec35c_ab8dd0b6 :
PS2, Line 1748: LSTATUS err = RegOpenKeyExA(HKEY_LOCAL_MACHINE, itfs_key, 0, KEY_ALL_ACCESS, key);
Quoted 1 line
Here we use RegOpenKeyExA, and in the function below RegOpenKeyExW
Yes, it is no problem. I always switch between A and W with the registry functions depending on either the subkey string (second argument) or in which format the data is that I write or which format I want when I read. It is mostly motivated by the possibility of the data containing non-ASCII characters.
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id2bed0908e84c19b8fb6fe806376316793e550b4
Gerrit-Change-Number: 825
Gerrit-PatchSet: 2
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-CC: stipa <[email protected]...>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: stipa <[email protected]...>
Gerrit-Comment-Date: Wed, 04 Dec 2024 22:12:35 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: stipa <[email protected]...>
Gerrit-MessageType: comment
Attention is currently required from: d12fk, flichtenheld, plaisthos.
stipa has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/825?usp=email )
Change subject: dns: do not use netsh to set name server addresses
......................................................................
Patch Set 2:
(1 comment)
File src/openvpnserv/interactive.c:
http://gerrit.openvpn.net/c/openvpn/+/825/comment/8ee2951f_d0d9d3e3 :
PS2, Line 1748: LSTATUS err = RegOpenKeyExA(HKEY_LOCAL_MACHINE, itfs_key, 0, KEY_ALL_ACCESS, key);
Quoted 1 line
Yes, it is no problem. […]
I see, it is just before we always either explicitly used W version or macro which is resolved into W. But since we pass DNS as a char string we use A version.
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id2bed0908e84c19b8fb6fe806376316793e550b4
Gerrit-Change-Number: 825
Gerrit-PatchSet: 2
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-CC: stipa <[email protected]...>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Comment-Date: Thu, 05 Dec 2024 11:46:51 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: d12fk <[email protected]...>
Comment-In-Reply-To: stipa <[email protected]...>
Gerrit-MessageType: comment
Attention is currently required from: d12fk, flichtenheld, plaisthos.
stipa has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/825?usp=email )
Change subject: dns: do not use netsh to set name server addresses
......................................................................
Patch Set 2: Code-Review+2
(1 comment)
Patchset:
PS2:
Looked and the code and tested, it indeed sets DNS server address on a adapter specific key in the registry.
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id2bed0908e84c19b8fb6fe806376316793e550b4
Gerrit-Change-Number: 825
Gerrit-PatchSet: 2
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-Reviewer: stipa <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Comment-Date: Mon, 09 Dec 2024 10:00:17 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment
Attention is currently required from: d12fk, flichtenheld, plaisthos.
Hello flichtenheld, plaisthos, stipa,
I'd like you to reexamine a change. Please visit
http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
to look at the new patch set (#3).
The change is no longer submittable: checks~ChecksSubmitRule is unsatisfied now.
Change subject: dns: do not use netsh to set name server addresses
......................................................................
dns: do not use netsh to set name server addresses
Instead of spawning a netsh process, set the name server addresses directly in the registry hive of the VPN interface.
This is a first step to get rid of the use of command line tools in the service and move to a more API driven style of modifying the VPN adapter configuration.
---
M src/openvpnserv/interactive.c
1 file changed, 146 insertions(+), 115 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/25/825/3
src/openvpnserv/interactive.c +146 -115
@@ -1024,65 +1024,6 @@ }/** -* Run the command: netsh interface $proto $action dns $if_name $addr [validate=no] -* @param action "delete" or "add" -* @param proto "ipv6" or "ip" -* @param if_name "name_of_interface" -* @param addr IPv4 (for proto = ip) or IPv6 address as a string -* -* If addr is null and action = "delete" all addresses are deleted. -*/ -static DWORD -netsh_dns_cmd (const wchar_t *action,const wchar_t *proto,const wchar_t *if_name,const wchar_t *addr) -{ -DWORD err =0 ; -int timeout =30000 ;/* in msec */ -wchar_t argv0[MAX_PATH ]; -wchar_t *cmdline =NULL ; - -if (!addr) - { -if (wcscmp (action,L"delete" ) ==0 ) - { - addr =L"all" ; - } -else /* nothing to do -- return success*/ - { - goto out; - } - } - -/* Path of netsh */ -swprintf (argv0,_countof (argv0),L"%ls\\%ls" ,get_win_sys_path (),L"netsh.exe" ); - -/* cmd template: -* netsh interface $proto $action dns $if_name $addr [validate=no] -*/ -const wchar_t *fmt =L"netsh interface %ls %ls dns \"%ls\" %ls" ; - -/* max cmdline length in wchars -- include room for worst case and some */ -size_t ncmdline =wcslen (fmt) +wcslen (if_name) +wcslen (addr) +32 +1 ; - cmdline =malloc (ncmdline*sizeof (wchar_t)); -if (!cmdline) - { - err =ERROR_OUTOFMEMORY ; - goto out; - } - -swprintf (cmdline, ncmdline, fmt, proto, action, if_name, addr); - -if (IsWindows7OrGreater ()) - { -wcscat_s (cmdline, ncmdline,L" validate=no" ); - } - err =ExecCommand (argv0, cmdline, timeout); - -out: -free (cmdline); -return err; -} - -/** * Run the command: netsh interface ip $action wins $if_name [static] $addr * @param action "delete", "add" or "set" * @param if_name "name_of_interface"
@@ -1139,22 +1080,6 @@return err; } -/* Delete all IPv4 or IPv6 dns servers for an interface */ -static DWORD -DeleteDNS (short family,wchar_t *if_name) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"delete" , proto, if_name,NULL ); -} - -/* Add an IPv4 or IPv6 dns server to an interface */ -static DWORD -AddDNS (short family,wchar_t *if_name,wchar_t *addr) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"add" , proto, if_name, addr); -} -static BOOL CmpWString (LPVOID item,LPVOID str) {
@@ -1812,11 +1737,115 @@return err; } +/** +* Return the interfaces registry key for the specified address family +* +* @param family the internet address family to open the key for +* @param key PHKEY to return the key in +* @return BOOL to indicate success or failure +*/ +static BOOL +GetInterfacesKey (short family,PHKEY key) +{ +PCSTR itfs_key = family ==AF_INET6 + ?"SYSTEM\\CurrentControlSet\\Services\\Tcpip6\\Parameters\\Interfaces" + :"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces" ; + +LSTATUS err =RegOpenKeyExA (HKEY_LOCAL_MACHINE , itfs_key,0 ,KEY_ALL_ACCESS , key); +if (err) + { + *key =INVALID_HANDLE_VALUE ; +MsgToEventLog (M_SYSERR ,TEXT ("GetInterfacesKey: " +"could not open interfaces registry key for family %d (%lu)" ), + family, err); + } + +return err ? FALSE : TRUE; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param value the value to set the name servers to +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServersValue (PCWSTR itf_id,short family,PCSTR value) +{ +DWORD err; + +HKEY itfs; +if (!GetInterfacesKey (family, &itfs)) + { +return ERROR_FILE_NOT_FOUND ; + } + +HKEY itf =INVALID_HANDLE_VALUE ; + err =RegOpenKeyExW (itfs, itf_id,0 ,KEY_ALL_ACCESS , &itf); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not open interface key for %s family %d (%lu)" ), + itf_id, family, err); + goto out; + } + + err =RegSetValueExA (itf,"NameServer" ,0 ,REG_SZ , (PBYTE )value,strlen (value) +1 ); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not set name servers '%S' for %s family %d (%lu)" ), + value, itf_id, family, err); + } + +out: +if (itf !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itf); + } +if (itfs !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itfs); + } +return err; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param addrs comma separated list of name server addresses +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServers (PCWSTR itf_id,short family,PCSTR addrs) +{ +return SetNameServersValue (itf_id, family, addrs); +} + +/** +* Delete all DNS name servers from a registry interface configuration +* +* @param itf_id the interface id to clear the servers for +* @param family internet address family to clear the servers for +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +ResetNameServers (PCWSTR itf_id,short family) +{ +return SetNameServersValue (itf_id, family,"" ); +} +static DWORD HandleDNSConfigMessage (const dns_cfg_message_t *msg,undo_lists_t *lists) {DWORD err =0 ; -wchar_t addr[46 ];/* large enough to hold string representation of an ipv4 / ipv6 address */ undo_type_t undo_type = (msg->family ==AF_INET6 ) ? undo_dns4 : undo_dns6;int addr_len = msg->addr_len;
@@ -1838,10 +1867,11 @@ msgptr->domains[_countof (msg->domains)-1 ] ='\0' ; } -wchar_t *wide_name =utf8to16 (msg->iface.name);/* utf8 to wide-char */ -if (!wide_name) +WCHAR iid[64 ]; + err =InterfaceIdString (msg->iface.name, iid,_countof (iid)); +if (err) { -return ERROR_OUTOFMEMORY ; +return err; }/* We delete all current addresses before adding any
@@ -1849,12 +1879,12 @@*/ if (addr_len >0 || msg->header.type == msg_del_dns_cfg) { - err =DeleteDNS (msg->family, wide_name); + err =ResetNameServers (iid, msg->family);if (err) { - goto out; +return err; } -free (RemoveListItem (&(*lists)[undo_type], CmpWString, wide_name)); +free (RemoveListItem (&(*lists)[undo_type], CmpAny, iid)); }if (msg->header.type == msg_del_dns_cfg)
@@ -1866,40 +1896,43 @@ err =SetDnsSearchDomains (msg->iface.name,NULL , &gpol, lists); }ApplyDnsSettings (gpol); - goto out;/* job done */ +return err;/* job done */ } -for (int i =0 ; i < addr_len; ++i) - { -if (msg->family ==AF_INET6 ) - { -RtlIpv6AddressToStringW (&msg->addr[i].ipv6, addr); - } -else - { -RtlIpv4AddressToStringW (&msg->addr[i].ipv4, addr); - } - err =AddDNS (msg->family, wide_name, addr); -if (i ==0 && err) - { - goto out; - } -/* We do not check for duplicate addresses, so any error in adding -* additional addresses is ignored. -*/ - } - - err =0 ; -if (msg->addr_len >0 ) { -wchar_t *tmp_name =_wcsdup (wide_name); -if (!tmp_name ||AddListItem (&(*lists)[undo_type], tmp_name)) +/* prepare the comma separated address list */ +CHAR addrs[256 ];/* large enough to hold four IPv4 / IPv6 address strings */ +size_t offset =0 ; +for (int i =0 ; i < addr_len; ++i) { -free (tmp_name); -DeleteDNS (msg->family, wide_name); - err =ERROR_OUTOFMEMORY ; - goto out; +if (i !=0 ) + { + addrs[offset++] =',' ; + } +if (msg->family ==AF_INET6 ) + { +RtlIpv6AddressToStringA (&msg->addr[i].ipv6, addrs + offset); + } +else + { +RtlIpv4AddressToStringA (&msg->addr[i].ipv4, addrs + offset); + } + offset +=strlen (addrs); + } + + err =SetNameServers (iid, msg->family, addrs); +if (err) + { +return err; + } + +wchar_t *tmp_iid =_wcsdup (iid); +if (!tmp_iid ||AddListItem (&(*lists)[undo_type], tmp_iid)) + { +free (tmp_iid); +ResetNameServers (iid, msg->family); +return ERROR_OUTOFMEMORY ; } }
@@ -1910,8 +1943,6 @@ }ApplyDnsSettings (gpol); -out: -free (wide_name);return err; }
@@ -2292,11 +2323,11 @@break ;case undo_dns4: -DeleteDNS (AF_INET , item->data); +ResetNameServers (item->data,AF_INET );break ;case undo_dns6: -DeleteDNS (AF_INET6 , item->data); +ResetNameServers (item->data,AF_INET6 );break ;break ;
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id2bed0908e84c19b8fb6fe806376316793e550b4
Gerrit-Change-Number: 825
Gerrit-PatchSet: 3
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-Reviewer: stipa <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-MessageType: newpatchset
Attention is currently required from: d12fk, flichtenheld, plaisthos.
Hello flichtenheld, plaisthos, stipa,
I'd like you to reexamine a change. Please visit
http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
to look at the new patch set (#5).
The change is no longer submittable: checks~ChecksSubmitRule is unsatisfied now.
Change subject: dns: do not use netsh to set name server addresses
......................................................................
dns: do not use netsh to set name server addresses
Instead of spawning a netsh process, set the name server addresses directly in the registry hive of the VPN interface.
This is a first step to get rid of the use of command line tools in the service and move to a more API driven style of modifying the VPN adapter configuration.
---
M src/openvpnserv/interactive.c
1 file changed, 146 insertions(+), 115 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/25/825/5
src/openvpnserv/interactive.c +146 -115
@@ -1024,65 +1024,6 @@ }/** -* Run the command: netsh interface $proto $action dns $if_name $addr [validate=no] -* @param action "delete" or "add" -* @param proto "ipv6" or "ip" -* @param if_name "name_of_interface" -* @param addr IPv4 (for proto = ip) or IPv6 address as a string -* -* If addr is null and action = "delete" all addresses are deleted. -*/ -static DWORD -netsh_dns_cmd (const wchar_t *action,const wchar_t *proto,const wchar_t *if_name,const wchar_t *addr) -{ -DWORD err =0 ; -int timeout =30000 ;/* in msec */ -wchar_t argv0[MAX_PATH ]; -wchar_t *cmdline =NULL ; - -if (!addr) - { -if (wcscmp (action,L"delete" ) ==0 ) - { - addr =L"all" ; - } -else /* nothing to do -- return success*/ - { - goto out; - } - } - -/* Path of netsh */ -swprintf (argv0,_countof (argv0),L"%ls\\%ls" ,get_win_sys_path (),L"netsh.exe" ); - -/* cmd template: -* netsh interface $proto $action dns $if_name $addr [validate=no] -*/ -const wchar_t *fmt =L"netsh interface %ls %ls dns \"%ls\" %ls" ; - -/* max cmdline length in wchars -- include room for worst case and some */ -size_t ncmdline =wcslen (fmt) +wcslen (if_name) +wcslen (addr) +32 +1 ; - cmdline =malloc (ncmdline*sizeof (wchar_t)); -if (!cmdline) - { - err =ERROR_OUTOFMEMORY ; - goto out; - } - -swprintf (cmdline, ncmdline, fmt, proto, action, if_name, addr); - -if (IsWindows7OrGreater ()) - { -wcscat_s (cmdline, ncmdline,L" validate=no" ); - } - err =ExecCommand (argv0, cmdline, timeout); - -out: -free (cmdline); -return err; -} - -/** * Run the command: netsh interface ip $action wins $if_name [static] $addr * @param action "delete", "add" or "set" * @param if_name "name_of_interface"
@@ -1139,22 +1080,6 @@return err; } -/* Delete all IPv4 or IPv6 dns servers for an interface */ -static DWORD -DeleteDNS (short family,wchar_t *if_name) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"delete" , proto, if_name,NULL ); -} - -/* Add an IPv4 or IPv6 dns server to an interface */ -static DWORD -AddDNS (short family,wchar_t *if_name,wchar_t *addr) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"add" , proto, if_name, addr); -} -static BOOL CmpWString (LPVOID item,LPVOID str) {
@@ -1812,11 +1737,115 @@return err; } +/** +* Return the interfaces registry key for the specified address family +* +* @param family the internet address family to open the key for +* @param key PHKEY to return the key in +* @return BOOL to indicate success or failure +*/ +static BOOL +GetInterfacesKey (short family,PHKEY key) +{ +PCSTR itfs_key = family ==AF_INET6 + ?"SYSTEM\\CurrentControlSet\\Services\\Tcpip6\\Parameters\\Interfaces" + :"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces" ; + +LSTATUS err =RegOpenKeyExA (HKEY_LOCAL_MACHINE , itfs_key,0 ,KEY_ALL_ACCESS , key); +if (err) + { + *key =INVALID_HANDLE_VALUE ; +MsgToEventLog (M_SYSERR ,TEXT ("GetInterfacesKey: " +"could not open interfaces registry key for family %d (%lu)" ), + family, err); + } + +return err ? FALSE : TRUE; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param value the value to set the name servers to +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServersValue (PCWSTR itf_id,short family,PCSTR value) +{ +DWORD err; + +HKEY itfs; +if (!GetInterfacesKey (family, &itfs)) + { +return ERROR_FILE_NOT_FOUND ; + } + +HKEY itf =INVALID_HANDLE_VALUE ; + err =RegOpenKeyExW (itfs, itf_id,0 ,KEY_ALL_ACCESS , &itf); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not open interface key for %s family %d (%lu)" ), + itf_id, family, err); + goto out; + } + + err =RegSetValueExA (itf,"NameServer" ,0 ,REG_SZ , (PBYTE )value,strlen (value) +1 ); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not set name servers '%S' for %s family %d (%lu)" ), + value, itf_id, family, err); + } + +out: +if (itf !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itf); + } +if (itfs !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itfs); + } +return err; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param addrs comma separated list of name server addresses +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServers (PCWSTR itf_id,short family,PCSTR addrs) +{ +return SetNameServersValue (itf_id, family, addrs); +} + +/** +* Delete all DNS name servers from a registry interface configuration +* +* @param itf_id the interface id to clear the servers for +* @param family internet address family to clear the servers for +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +ResetNameServers (PCWSTR itf_id,short family) +{ +return SetNameServersValue (itf_id, family,"" ); +} +static DWORD HandleDNSConfigMessage (const dns_cfg_message_t *msg,undo_lists_t *lists) {DWORD err =0 ; -wchar_t addr[46 ];/* large enough to hold string representation of an ipv4 / ipv6 address */ undo_type_t undo_type = (msg->family ==AF_INET6 ) ? undo_dns4 : undo_dns6;int addr_len = msg->addr_len;
@@ -1838,10 +1867,11 @@ msgptr->domains[_countof (msg->domains)-1 ] ='\0' ; } -wchar_t *wide_name =utf8to16 (msg->iface.name);/* utf8 to wide-char */ -if (!wide_name) +WCHAR iid[64 ]; + err =InterfaceIdString (msg->iface.name, iid,_countof (iid)); +if (err) { -return ERROR_OUTOFMEMORY ; +return err; }/* We delete all current addresses before adding any
@@ -1849,12 +1879,12 @@*/ if (addr_len >0 || msg->header.type == msg_del_dns_cfg) { - err =DeleteDNS (msg->family, wide_name); + err =ResetNameServers (iid, msg->family);if (err) { - goto out; +return err; } -free (RemoveListItem (&(*lists)[undo_type], CmpWString, wide_name)); +free (RemoveListItem (&(*lists)[undo_type], CmpAny, iid)); }if (msg->header.type == msg_del_dns_cfg)
@@ -1866,40 +1896,43 @@ err =SetDnsSearchDomains (msg->iface.name,NULL , &gpol, lists); }ApplyDnsSettings (gpol); - goto out;/* job done */ +return err;/* job done */ } -for (int i =0 ; i < addr_len; ++i) - { -if (msg->family ==AF_INET6 ) - { -RtlIpv6AddressToStringW (&msg->addr[i].ipv6, addr); - } -else - { -RtlIpv4AddressToStringW (&msg->addr[i].ipv4, addr); - } - err =AddDNS (msg->family, wide_name, addr); -if (i ==0 && err) - { - goto out; - } -/* We do not check for duplicate addresses, so any error in adding -* additional addresses is ignored. -*/ - } - - err =0 ; -if (msg->addr_len >0 ) { -wchar_t *tmp_name =_wcsdup (wide_name); -if (!tmp_name ||AddListItem (&(*lists)[undo_type], tmp_name)) +/* prepare the comma separated address list */ +CHAR addrs[256 ];/* large enough to hold four IPv4 / IPv6 address strings */ +size_t offset =0 ; +for (int i =0 ; i < addr_len; ++i) { -free (tmp_name); -DeleteDNS (msg->family, wide_name); - err =ERROR_OUTOFMEMORY ; - goto out; +if (i !=0 ) + { + addrs[offset++] =',' ; + } +if (msg->family ==AF_INET6 ) + { +RtlIpv6AddressToStringA (&msg->addr[i].ipv6, addrs + offset); + } +else + { +RtlIpv4AddressToStringA (&msg->addr[i].ipv4, addrs + offset); + } + offset +=strlen (addrs); + } + + err =SetNameServers (iid, msg->family, addrs); +if (err) + { +return err; + } + +wchar_t *tmp_iid =_wcsdup (iid); +if (!tmp_iid ||AddListItem (&(*lists)[undo_type], tmp_iid)) + { +free (tmp_iid); +ResetNameServers (iid, msg->family); +return ERROR_OUTOFMEMORY ; } }
@@ -1910,8 +1943,6 @@ }ApplyDnsSettings (gpol); -out: -free (wide_name);return err; }
@@ -2292,11 +2323,11 @@break ;case undo_dns4: -DeleteDNS (AF_INET , item->data); +ResetNameServers (item->data,AF_INET );break ;case undo_dns6: -DeleteDNS (AF_INET6 , item->data); +ResetNameServers (item->data,AF_INET6 );break ;case undo_domains:
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id2bed0908e84c19b8fb6fe806376316793e550b4
Gerrit-Change-Number: 825
Gerrit-PatchSet: 5
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-Reviewer: stipa <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-MessageType: newpatchset
Attention is currently required from: d12fk, flichtenheld, plaisthos.
Hello flichtenheld, plaisthos, stipa,
I'd like you to reexamine a change. Please visit
http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
to look at the new patch set (#6).
The change is no longer submittable: checks~ChecksSubmitRule is unsatisfied now.
Change subject: dns: do not use netsh to set name server addresses
......................................................................
dns: do not use netsh to set name server addresses
Instead of spawning a netsh process, set the name server addresses directly in the registry hive of the VPN interface.
This is a first step to get rid of the use of command line tools in the service and move to a more API driven style of modifying the VPN adapter configuration.
---
M src/openvpnserv/interactive.c
1 file changed, 146 insertions(+), 115 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/25/825/6
src/openvpnserv/interactive.c +146 -115
@@ -1024,65 +1024,6 @@ }/** -* Run the command: netsh interface $proto $action dns $if_name $addr [validate=no] -* @param action "delete" or "add" -* @param proto "ipv6" or "ip" -* @param if_name "name_of_interface" -* @param addr IPv4 (for proto = ip) or IPv6 address as a string -* -* If addr is null and action = "delete" all addresses are deleted. -*/ -static DWORD -netsh_dns_cmd (const wchar_t *action,const wchar_t *proto,const wchar_t *if_name,const wchar_t *addr) -{ -DWORD err =0 ; -int timeout =30000 ;/* in msec */ -wchar_t argv0[MAX_PATH ]; -wchar_t *cmdline =NULL ; - -if (!addr) - { -if (wcscmp (action,L"delete" ) ==0 ) - { - addr =L"all" ; - } -else /* nothing to do -- return success*/ - { - goto out; - } - } - -/* Path of netsh */ -swprintf (argv0,_countof (argv0),L"%ls\\%ls" ,get_win_sys_path (),L"netsh.exe" ); - -/* cmd template: -* netsh interface $proto $action dns $if_name $addr [validate=no] -*/ -const wchar_t *fmt =L"netsh interface %ls %ls dns \"%ls\" %ls" ; - -/* max cmdline length in wchars -- include room for worst case and some */ -size_t ncmdline =wcslen (fmt) +wcslen (if_name) +wcslen (addr) +32 +1 ; - cmdline =malloc (ncmdline*sizeof (wchar_t)); -if (!cmdline) - { - err =ERROR_OUTOFMEMORY ; - goto out; - } - -swprintf (cmdline, ncmdline, fmt, proto, action, if_name, addr); - -if (IsWindows7OrGreater ()) - { -wcscat_s (cmdline, ncmdline,L" validate=no" ); - } - err =ExecCommand (argv0, cmdline, timeout); - -out: -free (cmdline); -return err; -} - -/** * Run the command: netsh interface ip $action wins $if_name [static] $addr * @param action "delete", "add" or "set" * @param if_name "name_of_interface"
@@ -1139,22 +1080,6 @@return err; } -/* Delete all IPv4 or IPv6 dns servers for an interface */ -static DWORD -DeleteDNS (short family,wchar_t *if_name) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"delete" , proto, if_name,NULL ); -} - -/* Add an IPv4 or IPv6 dns server to an interface */ -static DWORD -AddDNS (short family,wchar_t *if_name,wchar_t *addr) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"add" , proto, if_name, addr); -} -static BOOL CmpWString (LPVOID item,LPVOID str) {
@@ -1815,11 +1740,115 @@return err; } +/** +* Return the interfaces registry key for the specified address family +* +* @param family the internet address family to open the key for +* @param key PHKEY to return the key in +* @return BOOL to indicate success or failure +*/ +static BOOL +GetInterfacesKey (short family,PHKEY key) +{ +PCSTR itfs_key = family ==AF_INET6 + ?"SYSTEM\\CurrentControlSet\\Services\\Tcpip6\\Parameters\\Interfaces" + :"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces" ; + +LSTATUS err =RegOpenKeyExA (HKEY_LOCAL_MACHINE , itfs_key,0 ,KEY_ALL_ACCESS , key); +if (err) + { + *key =INVALID_HANDLE_VALUE ; +MsgToEventLog (M_SYSERR ,TEXT ("GetInterfacesKey: " +"could not open interfaces registry key for family %d (%lu)" ), + family, err); + } + +return err ? FALSE : TRUE; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param value the value to set the name servers to +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServersValue (PCWSTR itf_id,short family,PCSTR value) +{ +DWORD err; + +HKEY itfs; +if (!GetInterfacesKey (family, &itfs)) + { +return ERROR_FILE_NOT_FOUND ; + } + +HKEY itf =INVALID_HANDLE_VALUE ; + err =RegOpenKeyExW (itfs, itf_id,0 ,KEY_ALL_ACCESS , &itf); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not open interface key for %s family %d (%lu)" ), + itf_id, family, err); + goto out; + } + + err =RegSetValueExA (itf,"NameServer" ,0 ,REG_SZ , (PBYTE )value,strlen (value) +1 ); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not set name servers '%S' for %s family %d (%lu)" ), + value, itf_id, family, err); + } + +out: +if (itf !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itf); + } +if (itfs !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itfs); + } +return err; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param addrs comma separated list of name server addresses +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServers (PCWSTR itf_id,short family,PCSTR addrs) +{ +return SetNameServersValue (itf_id, family, addrs); +} + +/** +* Delete all DNS name servers from a registry interface configuration +* +* @param itf_id the interface id to clear the servers for +* @param family internet address family to clear the servers for +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +ResetNameServers (PCWSTR itf_id,short family) +{ +return SetNameServersValue (itf_id, family,"" ); +} +static DWORD HandleDNSConfigMessage (const dns_cfg_message_t *msg,undo_lists_t *lists) {DWORD err =0 ; -wchar_t addr[46 ];/* large enough to hold string representation of an ipv4 / ipv6 address */ undo_type_t undo_type = (msg->family ==AF_INET6 ) ? undo_dns4 : undo_dns6;int addr_len = msg->addr_len;
@@ -1841,10 +1870,11 @@ msgptr->domains[_countof (msg->domains)-1 ] ='\0' ; } -wchar_t *wide_name =utf8to16 (msg->iface.name);/* utf8 to wide-char */ -if (!wide_name) +WCHAR iid[64 ]; + err =InterfaceIdString (msg->iface.name, iid,_countof (iid)); +if (err) { -return ERROR_OUTOFMEMORY ; +return err; }/* We delete all current addresses before adding any
@@ -1852,12 +1882,12 @@*/ if (addr_len >0 || msg->header.type == msg_del_dns_cfg) { - err =DeleteDNS (msg->family, wide_name); + err =ResetNameServers (iid, msg->family);if (err) { - goto out; +return err; } -free (RemoveListItem (&(*lists)[undo_type], CmpWString, wide_name)); +free (RemoveListItem (&(*lists)[undo_type], CmpAny, iid)); }if (msg->header.type == msg_del_dns_cfg)
@@ -1869,40 +1899,43 @@ err =SetDnsSearchDomains (msg->iface.name,NULL , &gpol, lists); }ApplyDnsSettings (gpol); - goto out;/* job done */ +return err;/* job done */ } -for (int i =0 ; i < addr_len; ++i) - { -if (msg->family ==AF_INET6 ) - { -RtlIpv6AddressToStringW (&msg->addr[i].ipv6, addr); - } -else - { -RtlIpv4AddressToStringW (&msg->addr[i].ipv4, addr); - } - err =AddDNS (msg->family, wide_name, addr); -if (i ==0 && err) - { - goto out; - } -/* We do not check for duplicate addresses, so any error in adding -* additional addresses is ignored. -*/ - } - - err =0 ; -if (msg->addr_len >0 ) { -wchar_t *tmp_name =_wcsdup (wide_name); -if (!tmp_name ||AddListItem (&(*lists)[undo_type], tmp_name)) +/* prepare the comma separated address list */ +CHAR addrs[256 ];/* large enough to hold four IPv4 / IPv6 address strings */ +size_t offset =0 ; +for (int i =0 ; i < addr_len; ++i) { -free (tmp_name); -DeleteDNS (msg->family, wide_name); - err =ERROR_OUTOFMEMORY ; - goto out; +if (i !=0 ) + { + addrs[offset++] =',' ; + } +if (msg->family ==AF_INET6 ) + { +RtlIpv6AddressToStringA (&msg->addr[i].ipv6, addrs + offset); + } +else + { +RtlIpv4AddressToStringA (&msg->addr[i].ipv4, addrs + offset); + } + offset +=strlen (addrs); + } + + err =SetNameServers (iid, msg->family, addrs); +if (err) + { +return err; + } + +wchar_t *tmp_iid =_wcsdup (iid); +if (!tmp_iid ||AddListItem (&(*lists)[undo_type], tmp_iid)) + { +free (tmp_iid); +ResetNameServers (iid, msg->family); +return ERROR_OUTOFMEMORY ; } }
@@ -1913,8 +1946,6 @@ }ApplyDnsSettings (gpol); -out: -free (wide_name);return err; }
@@ -2295,11 +2326,11 @@break ;case undo_dns4: -DeleteDNS (AF_INET , item->data); +ResetNameServers (item->data,AF_INET );break ;case undo_dns6: -DeleteDNS (AF_INET6 , item->data); +ResetNameServers (item->data,AF_INET6 );break ;case undo_domains:
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id2bed0908e84c19b8fb6fe806376316793e550b4
Gerrit-Change-Number: 825
Gerrit-PatchSet: 6
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-Reviewer: stipa <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-MessageType: newpatchset
Attention is currently required from: d12fk, flichtenheld, plaisthos.
Hello flichtenheld, plaisthos, stipa,
I'd like you to reexamine a change. Please visit
http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
to look at the new patch set (#7).
The change is no longer submittable: checks~ChecksSubmitRule is unsatisfied now.
Change subject: dns: do not use netsh to set name server addresses
......................................................................
dns: do not use netsh to set name server addresses
Instead of spawning a netsh process, set the name server addresses directly in the registry hive of the VPN interface.
This is a first step to get rid of the use of command line tools in the service and move to a more API driven style of modifying the VPN adapter configuration.
---
M src/openvpnserv/interactive.c
1 file changed, 146 insertions(+), 115 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/25/825/7
src/openvpnserv/interactive.c +146 -115
@@ -1024,65 +1024,6 @@ }/** -* Run the command: netsh interface $proto $action dns $if_name $addr [validate=no] -* @param action "delete" or "add" -* @param proto "ipv6" or "ip" -* @param if_name "name_of_interface" -* @param addr IPv4 (for proto = ip) or IPv6 address as a string -* -* If addr is null and action = "delete" all addresses are deleted. -*/ -static DWORD -netsh_dns_cmd (const wchar_t *action,const wchar_t *proto,const wchar_t *if_name,const wchar_t *addr) -{ -DWORD err =0 ; -int timeout =30000 ;/* in msec */ -wchar_t argv0[MAX_PATH ]; -wchar_t *cmdline =NULL ; - -if (!addr) - { -if (wcscmp (action,L"delete" ) ==0 ) - { - addr =L"all" ; - } -else /* nothing to do -- return success*/ - { - goto out; - } - } - -/* Path of netsh */ -swprintf (argv0,_countof (argv0),L"%ls\\%ls" ,get_win_sys_path (),L"netsh.exe" ); - -/* cmd template: -* netsh interface $proto $action dns $if_name $addr [validate=no] -*/ -const wchar_t *fmt =L"netsh interface %ls %ls dns \"%ls\" %ls" ; - -/* max cmdline length in wchars -- include room for worst case and some */ -size_t ncmdline =wcslen (fmt) +wcslen (if_name) +wcslen (addr) +32 +1 ; - cmdline =malloc (ncmdline*sizeof (wchar_t)); -if (!cmdline) - { - err =ERROR_OUTOFMEMORY ; - goto out; - } - -swprintf (cmdline, ncmdline, fmt, proto, action, if_name, addr); - -if (IsWindows7OrGreater ()) - { -wcscat_s (cmdline, ncmdline,L" validate=no" ); - } - err =ExecCommand (argv0, cmdline, timeout); - -out: -free (cmdline); -return err; -} - -/** * Run the command: netsh interface ip $action wins $if_name [static] $addr * @param action "delete", "add" or "set" * @param if_name "name_of_interface"
@@ -1139,22 +1080,6 @@return err; } -/* Delete all IPv4 or IPv6 dns servers for an interface */ -static DWORD -DeleteDNS (short family,wchar_t *if_name) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"delete" , proto, if_name,NULL ); -} - -/* Add an IPv4 or IPv6 dns server to an interface */ -static DWORD -AddDNS (short family,wchar_t *if_name,wchar_t *addr) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"add" , proto, if_name, addr); -} -static BOOL CmpWString (LPVOID item,LPVOID str) {
@@ -1815,11 +1740,115 @@return err; } +/** +* Return the interfaces registry key for the specified address family +* +* @param family the internet address family to open the key for +* @param key PHKEY to return the key in +* @return BOOL to indicate success or failure +*/ +static BOOL +GetInterfacesKey (short family,PHKEY key) +{ +PCSTR itfs_key = family ==AF_INET6 + ?"SYSTEM\\CurrentControlSet\\Services\\Tcpip6\\Parameters\\Interfaces" + :"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces" ; + +LSTATUS err =RegOpenKeyExA (HKEY_LOCAL_MACHINE , itfs_key,0 ,KEY_ALL_ACCESS , key); +if (err) + { + *key =INVALID_HANDLE_VALUE ; +MsgToEventLog (M_SYSERR ,TEXT ("GetInterfacesKey: " +"could not open interfaces registry key for family %d (%lu)" ), + family, err); + } + +return err ? FALSE : TRUE; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param value the value to set the name servers to +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServersValue (PCWSTR itf_id,short family,PCSTR value) +{ +DWORD err; + +HKEY itfs; +if (!GetInterfacesKey (family, &itfs)) + { +return ERROR_FILE_NOT_FOUND ; + } + +HKEY itf =INVALID_HANDLE_VALUE ; + err =RegOpenKeyExW (itfs, itf_id,0 ,KEY_ALL_ACCESS , &itf); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not open interface key for %s family %d (%lu)" ), + itf_id, family, err); + goto out; + } + + err =RegSetValueExA (itf,"NameServer" ,0 ,REG_SZ , (PBYTE )value,strlen (value) +1 ); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not set name servers '%S' for %s family %d (%lu)" ), + value, itf_id, family, err); + } + +out: +if (itf !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itf); + } +if (itfs !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itfs); + } +return err; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param addrs comma separated list of name server addresses +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServers (PCWSTR itf_id,short family,PCSTR addrs) +{ +return SetNameServersValue (itf_id, family, addrs); +} + +/** +* Delete all DNS name servers from a registry interface configuration +* +* @param itf_id the interface id to clear the servers for +* @param family internet address family to clear the servers for +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +ResetNameServers (PCWSTR itf_id,short family) +{ +return SetNameServersValue (itf_id, family,"" ); +} +static DWORD HandleDNSConfigMessage (const dns_cfg_message_t *msg,undo_lists_t *lists) {DWORD err =0 ; -wchar_t addr[46 ];/* large enough to hold string representation of an ipv4 / ipv6 address */ undo_type_t undo_type = (msg->family ==AF_INET6 ) ? undo_dns4 : undo_dns6;int addr_len = msg->addr_len;
@@ -1841,10 +1870,11 @@ msgptr->domains[_countof (msg->domains)-1 ] ='\0' ; } -wchar_t *wide_name =utf8to16 (msg->iface.name);/* utf8 to wide-char */ -if (!wide_name) +WCHAR iid[64 ]; + err =InterfaceIdString (msg->iface.name, iid,_countof (iid)); +if (err) { -return ERROR_OUTOFMEMORY ; +return err; }/* We delete all current addresses before adding any
@@ -1852,12 +1882,12 @@*/ if (addr_len >0 || msg->header.type == msg_del_dns_cfg) { - err =DeleteDNS (msg->family, wide_name); + err =ResetNameServers (iid, msg->family);if (err) { - goto out; +return err; } -free (RemoveListItem (&(*lists)[undo_type], CmpWString, wide_name)); +free (RemoveListItem (&(*lists)[undo_type], CmpAny, iid)); }if (msg->header.type == msg_del_dns_cfg)
@@ -1869,40 +1899,43 @@ err =SetDnsSearchDomains (msg->iface.name,NULL , &gpol, lists); }ApplyDnsSettings (gpol); - goto out;/* job done */ +return err;/* job done */ } -for (int i =0 ; i < addr_len; ++i) - { -if (msg->family ==AF_INET6 ) - { -RtlIpv6AddressToStringW (&msg->addr[i].ipv6, addr); - } -else - { -RtlIpv4AddressToStringW (&msg->addr[i].ipv4, addr); - } - err =AddDNS (msg->family, wide_name, addr); -if (i ==0 && err) - { - goto out; - } -/* We do not check for duplicate addresses, so any error in adding -* additional addresses is ignored. -*/ - } - - err =0 ; -if (msg->addr_len >0 ) { -wchar_t *tmp_name =_wcsdup (wide_name); -if (!tmp_name ||AddListItem (&(*lists)[undo_type], tmp_name)) +/* prepare the comma separated address list */ +CHAR addrs[256 ];/* large enough to hold four IPv4 / IPv6 address strings */ +size_t offset =0 ; +for (int i =0 ; i < addr_len; ++i) { -free (tmp_name); -DeleteDNS (msg->family, wide_name); - err =ERROR_OUTOFMEMORY ; - goto out; +if (i !=0 ) + { + addrs[offset++] =',' ; + } +if (msg->family ==AF_INET6 ) + { +RtlIpv6AddressToStringA (&msg->addr[i].ipv6, addrs + offset); + } +else + { +RtlIpv4AddressToStringA (&msg->addr[i].ipv4, addrs + offset); + } + offset +=strlen (addrs); + } + + err =SetNameServers (iid, msg->family, addrs); +if (err) + { +return err; + } + +wchar_t *tmp_iid =_wcsdup (iid); +if (!tmp_iid ||AddListItem (&(*lists)[undo_type], tmp_iid)) + { +free (tmp_iid); +ResetNameServers (iid, msg->family); +return ERROR_OUTOFMEMORY ; } }
@@ -1913,8 +1946,6 @@ }ApplyDnsSettings (gpol); -out: -free (wide_name);return err; }
@@ -2295,11 +2326,11 @@break ;case undo_dns4: -DeleteDNS (AF_INET , item->data); +ResetNameServers (item->data,AF_INET );break ;case undo_dns6: -DeleteDNS (AF_INET6 , item->data); +ResetNameServers (item->data,AF_INET6 );break ;case undo_domains:
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id2bed0908e84c19b8fb6fe806376316793e550b4
Gerrit-Change-Number: 825
Gerrit-PatchSet: 7
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-Reviewer: stipa <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-MessageType: newpatchset
Attention is currently required from: d12fk, flichtenheld, plaisthos.
Hello flichtenheld, plaisthos, stipa,
I'd like you to reexamine a change. Please visit
http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
to look at the new patch set (#8).
The change is no longer submittable: checks~ChecksSubmitRule is unsatisfied now.
Change subject: dns: do not use netsh to set name server addresses
......................................................................
dns: do not use netsh to set name server addresses
Instead of spawning a netsh process, set the name server addresses directly in the registry hive of the VPN interface.
This is a first step to get rid of the use of command line tools in the service and move to a more API driven style of modifying the VPN adapter configuration.
---
M src/openvpnserv/interactive.c
1 file changed, 146 insertions(+), 115 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/25/825/8
src/openvpnserv/interactive.c +146 -115
@@ -1024,65 +1024,6 @@ }/** -* Run the command: netsh interface $proto $action dns $if_name $addr [validate=no] -* @param action "delete" or "add" -* @param proto "ipv6" or "ip" -* @param if_name "name_of_interface" -* @param addr IPv4 (for proto = ip) or IPv6 address as a string -* -* If addr is null and action = "delete" all addresses are deleted. -*/ -static DWORD -netsh_dns_cmd (const wchar_t *action,const wchar_t *proto,const wchar_t *if_name,const wchar_t *addr) -{ -DWORD err =0 ; -int timeout =30000 ;/* in msec */ -wchar_t argv0[MAX_PATH ]; -wchar_t *cmdline =NULL ; - -if (!addr) - { -if (wcscmp (action,L"delete" ) ==0 ) - { - addr =L"all" ; - } -else /* nothing to do -- return success*/ - { - goto out; - } - } - -/* Path of netsh */ -swprintf (argv0,_countof (argv0),L"%ls\\%ls" ,get_win_sys_path (),L"netsh.exe" ); - -/* cmd template: -* netsh interface $proto $action dns $if_name $addr [validate=no] -*/ -const wchar_t *fmt =L"netsh interface %ls %ls dns \"%ls\" %ls" ; - -/* max cmdline length in wchars -- include room for worst case and some */ -size_t ncmdline =wcslen (fmt) +wcslen (if_name) +wcslen (addr) +32 +1 ; - cmdline =malloc (ncmdline*sizeof (wchar_t)); -if (!cmdline) - { - err =ERROR_OUTOFMEMORY ; - goto out; - } - -swprintf (cmdline, ncmdline, fmt, proto, action, if_name, addr); - -if (IsWindows7OrGreater ()) - { -wcscat_s (cmdline, ncmdline,L" validate=no" ); - } - err =ExecCommand (argv0, cmdline, timeout); - -out: -free (cmdline); -return err; -} - -/** * Run the command: netsh interface ip $action wins $if_name [static] $addr * @param action "delete", "add" or "set" * @param if_name "name_of_interface"
@@ -1139,22 +1080,6 @@return err; } -/* Delete all IPv4 or IPv6 dns servers for an interface */ -static DWORD -DeleteDNS (short family,wchar_t *if_name) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"delete" , proto, if_name,NULL ); -} - -/* Add an IPv4 or IPv6 dns server to an interface */ -static DWORD -AddDNS (short family,wchar_t *if_name,wchar_t *addr) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"add" , proto, if_name, addr); -} -static BOOL CmpWString (LPVOID item,LPVOID str) {
@@ -1818,11 +1743,115 @@return err; } +/** +* Return the interfaces registry key for the specified address family +* +* @param family the internet address family to open the key for +* @param key PHKEY to return the key in +* @return BOOL to indicate success or failure +*/ +static BOOL +GetInterfacesKey (short family,PHKEY key) +{ +PCSTR itfs_key = family ==AF_INET6 + ?"SYSTEM\\CurrentControlSet\\Services\\Tcpip6\\Parameters\\Interfaces" + :"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces" ; + +LSTATUS err =RegOpenKeyExA (HKEY_LOCAL_MACHINE , itfs_key,0 ,KEY_ALL_ACCESS , key); +if (err) + { + *key =INVALID_HANDLE_VALUE ; +MsgToEventLog (M_SYSERR ,TEXT ("GetInterfacesKey: " +"could not open interfaces registry key for family %d (%lu)" ), + family, err); + } + +return err ? FALSE : TRUE; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param value the value to set the name servers to +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServersValue (PCWSTR itf_id,short family,PCSTR value) +{ +DWORD err; + +HKEY itfs; +if (!GetInterfacesKey (family, &itfs)) + { +return ERROR_FILE_NOT_FOUND ; + } + +HKEY itf =INVALID_HANDLE_VALUE ; + err =RegOpenKeyExW (itfs, itf_id,0 ,KEY_ALL_ACCESS , &itf); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not open interface key for %s family %d (%lu)" ), + itf_id, family, err); + goto out; + } + + err =RegSetValueExA (itf,"NameServer" ,0 ,REG_SZ , (PBYTE )value,strlen (value) +1 ); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not set name servers '%S' for %s family %d (%lu)" ), + value, itf_id, family, err); + } + +out: +if (itf !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itf); + } +if (itfs !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itfs); + } +return err; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param addrs comma separated list of name server addresses +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServers (PCWSTR itf_id,short family,PCSTR addrs) +{ +return SetNameServersValue (itf_id, family, addrs); +} + +/** +* Delete all DNS name servers from a registry interface configuration +* +* @param itf_id the interface id to clear the servers for +* @param family internet address family to clear the servers for +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +ResetNameServers (PCWSTR itf_id,short family) +{ +return SetNameServersValue (itf_id, family,"" ); +} +static DWORD HandleDNSConfigMessage (const dns_cfg_message_t *msg,undo_lists_t *lists) {DWORD err =0 ; -wchar_t addr[46 ];/* large enough to hold string representation of an ipv4 / ipv6 address */ undo_type_t undo_type = (msg->family ==AF_INET6 ) ? undo_dns4 : undo_dns6;int addr_len = msg->addr_len;
@@ -1844,10 +1873,11 @@ msgptr->domains[_countof (msg->domains)-1 ] ='\0' ; } -wchar_t *wide_name =utf8to16 (msg->iface.name);/* utf8 to wide-char */ -if (!wide_name) +WCHAR iid[64 ]; + err =InterfaceIdString (msg->iface.name, iid,_countof (iid)); +if (err) { -return ERROR_OUTOFMEMORY ; +return err; }/* We delete all current addresses before adding any
@@ -1855,12 +1885,12 @@*/ if (addr_len >0 || msg->header.type == msg_del_dns_cfg) { - err =DeleteDNS (msg->family, wide_name); + err =ResetNameServers (iid, msg->family);if (err) { - goto out; +return err; } -free (RemoveListItem (&(*lists)[undo_type], CmpWString, wide_name)); +free (RemoveListItem (&(*lists)[undo_type], CmpAny, iid)); }if (msg->header.type == msg_del_dns_cfg)
@@ -1872,40 +1902,43 @@ err =SetDnsSearchDomains (msg->iface.name,NULL , &gpol, lists); }ApplyDnsSettings (gpol); - goto out;/* job done */ +return err;/* job done */ } -for (int i =0 ; i < addr_len; ++i) - { -if (msg->family ==AF_INET6 ) - { -RtlIpv6AddressToStringW (&msg->addr[i].ipv6, addr); - } -else - { -RtlIpv4AddressToStringW (&msg->addr[i].ipv4, addr); - } - err =AddDNS (msg->family, wide_name, addr); -if (i ==0 && err) - { - goto out; - } -/* We do not check for duplicate addresses, so any error in adding -* additional addresses is ignored. -*/ - } - - err =0 ; -if (msg->addr_len >0 ) { -wchar_t *tmp_name =_wcsdup (wide_name); -if (!tmp_name ||AddListItem (&(*lists)[undo_type], tmp_name)) +/* prepare the comma separated address list */ +CHAR addrs[256 ];/* large enough to hold four IPv4 / IPv6 address strings */ +size_t offset =0 ; +for (int i =0 ; i < addr_len; ++i) { -free (tmp_name); -DeleteDNS (msg->family, wide_name); - err =ERROR_OUTOFMEMORY ; - goto out; +if (i !=0 ) + { + addrs[offset++] =',' ; + } +if (msg->family ==AF_INET6 ) + { +RtlIpv6AddressToStringA (&msg->addr[i].ipv6, addrs + offset); + } +else + { +RtlIpv4AddressToStringA (&msg->addr[i].ipv4, addrs + offset); + } + offset +=strlen (addrs); + } + + err =SetNameServers (iid, msg->family, addrs); +if (err) + { +return err; + } + +wchar_t *tmp_iid =_wcsdup (iid); +if (!tmp_iid ||AddListItem (&(*lists)[undo_type], tmp_iid)) + { +free (tmp_iid); +ResetNameServers (iid, msg->family); +return ERROR_OUTOFMEMORY ; } }
@@ -1916,8 +1949,6 @@ }ApplyDnsSettings (gpol); -out: -free (wide_name);return err; }
@@ -2298,11 +2329,11 @@break ;case undo_dns4: -DeleteDNS (AF_INET , item->data); +ResetNameServers (item->data,AF_INET );break ;case undo_dns6: -DeleteDNS (AF_INET6 , item->data); +ResetNameServers (item->data,AF_INET6 );break ;case undo_domains:
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id2bed0908e84c19b8fb6fe806376316793e550b4
Gerrit-Change-Number: 825
Gerrit-PatchSet: 8
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-Reviewer: stipa <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-MessageType: newpatchset
Attention is currently required from: d12fk, flichtenheld, plaisthos.
Hello flichtenheld, plaisthos, stipa,
I'd like you to reexamine a change. Please visit
http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
to look at the new patch set (#9).
The change is no longer submittable: checks~ChecksSubmitRule is unsatisfied now.
Change subject: dns: do not use netsh to set name server addresses
......................................................................
dns: do not use netsh to set name server addresses
Instead of spawning a netsh process, set the name server addresses directly in the registry hive of the VPN interface.
This is a first step to get rid of the use of command line tools in the service and move to a more API driven style of modifying the VPN adapter configuration.
---
M src/openvpnserv/interactive.c
1 file changed, 146 insertions(+), 115 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/25/825/9
src/openvpnserv/interactive.c +146 -115
@@ -1024,65 +1024,6 @@ }/** -* Run the command: netsh interface $proto $action dns $if_name $addr [validate=no] -* @param action "delete" or "add" -* @param proto "ipv6" or "ip" -* @param if_name "name_of_interface" -* @param addr IPv4 (for proto = ip) or IPv6 address as a string -* -* If addr is null and action = "delete" all addresses are deleted. -*/ -static DWORD -netsh_dns_cmd (const wchar_t *action,const wchar_t *proto,const wchar_t *if_name,const wchar_t *addr) -{ -DWORD err =0 ; -int timeout =30000 ;/* in msec */ -wchar_t argv0[MAX_PATH ]; -wchar_t *cmdline =NULL ; - -if (!addr) - { -if (wcscmp (action,L"delete" ) ==0 ) - { - addr =L"all" ; - } -else /* nothing to do -- return success*/ - { - goto out; - } - } - -/* Path of netsh */ -swprintf (argv0,_countof (argv0),L"%ls\\%ls" ,get_win_sys_path (),L"netsh.exe" ); - -/* cmd template: -* netsh interface $proto $action dns $if_name $addr [validate=no] -*/ -const wchar_t *fmt =L"netsh interface %ls %ls dns \"%ls\" %ls" ; - -/* max cmdline length in wchars -- include room for worst case and some */ -size_t ncmdline =wcslen (fmt) +wcslen (if_name) +wcslen (addr) +32 +1 ; - cmdline =malloc (ncmdline*sizeof (wchar_t)); -if (!cmdline) - { - err =ERROR_OUTOFMEMORY ; - goto out; - } - -swprintf (cmdline, ncmdline, fmt, proto, action, if_name, addr); - -if (IsWindows7OrGreater ()) - { -wcscat_s (cmdline, ncmdline,L" validate=no" ); - } - err =ExecCommand (argv0, cmdline, timeout); - -out: -free (cmdline); -return err; -} - -/** * Run the command: netsh interface ip $action wins $if_name [static] $addr * @param action "delete", "add" or "set" * @param if_name "name_of_interface"
@@ -1139,22 +1080,6 @@return err; } -/* Delete all IPv4 or IPv6 dns servers for an interface */ -static DWORD -DeleteDNS (short family,wchar_t *if_name) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"delete" , proto, if_name,NULL ); -} - -/* Add an IPv4 or IPv6 dns server to an interface */ -static DWORD -AddDNS (short family,wchar_t *if_name,wchar_t *addr) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"add" , proto, if_name, addr); -} -static BOOL CmpWString (LPVOID item,LPVOID str) {
@@ -1818,11 +1743,115 @@return err; } +/** +* Return the interfaces registry key for the specified address family +* +* @param family the internet address family to open the key for +* @param key PHKEY to return the key in +* @return BOOL to indicate success or failure +*/ +static BOOL +GetInterfacesKey (short family,PHKEY key) +{ +PCSTR itfs_key = family ==AF_INET6 + ?"SYSTEM\\CurrentControlSet\\Services\\Tcpip6\\Parameters\\Interfaces" + :"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces" ; + +LSTATUS err =RegOpenKeyExA (HKEY_LOCAL_MACHINE , itfs_key,0 ,KEY_ALL_ACCESS , key); +if (err) + { + *key =INVALID_HANDLE_VALUE ; +MsgToEventLog (M_SYSERR ,TEXT ("GetInterfacesKey: " +"could not open interfaces registry key for family %d (%lu)" ), + family, err); + } + +return err ? FALSE : TRUE; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param value the value to set the name servers to +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServersValue (PCWSTR itf_id,short family,PCSTR value) +{ +DWORD err; + +HKEY itfs; +if (!GetInterfacesKey (family, &itfs)) + { +return ERROR_FILE_NOT_FOUND ; + } + +HKEY itf =INVALID_HANDLE_VALUE ; + err =RegOpenKeyExW (itfs, itf_id,0 ,KEY_ALL_ACCESS , &itf); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not open interface key for %s family %d (%lu)" ), + itf_id, family, err); + goto out; + } + + err =RegSetValueExA (itf,"NameServer" ,0 ,REG_SZ , (PBYTE )value,strlen (value) +1 ); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not set name servers '%S' for %s family %d (%lu)" ), + value, itf_id, family, err); + } + +out: +if (itf !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itf); + } +if (itfs !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itfs); + } +return err; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param addrs comma separated list of name server addresses +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServers (PCWSTR itf_id,short family,PCSTR addrs) +{ +return SetNameServersValue (itf_id, family, addrs); +} + +/** +* Delete all DNS name servers from a registry interface configuration +* +* @param itf_id the interface id to clear the servers for +* @param family internet address family to clear the servers for +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +ResetNameServers (PCWSTR itf_id,short family) +{ +return SetNameServersValue (itf_id, family,"" ); +} +static DWORD HandleDNSConfigMessage (const dns_cfg_message_t *msg,undo_lists_t *lists) {DWORD err =0 ; -wchar_t addr[46 ];/* large enough to hold string representation of an ipv4 / ipv6 address */ undo_type_t undo_type = (msg->family ==AF_INET6 ) ? undo_dns4 : undo_dns6;int addr_len = msg->addr_len;
@@ -1844,10 +1873,11 @@ msgptr->domains[_countof (msg->domains)-1 ] ='\0' ; } -wchar_t *wide_name =utf8to16 (msg->iface.name);/* utf8 to wide-char */ -if (!wide_name) +WCHAR iid[64 ]; + err =InterfaceIdString (msg->iface.name, iid,_countof (iid)); +if (err) { -return ERROR_OUTOFMEMORY ; +return err; }/* We delete all current addresses before adding any
@@ -1855,12 +1885,12 @@*/ if (addr_len >0 || msg->header.type == msg_del_dns_cfg) { - err =DeleteDNS (msg->family, wide_name); + err =ResetNameServers (iid, msg->family);if (err) { - goto out; +return err; } -free (RemoveListItem (&(*lists)[undo_type], CmpWString, wide_name)); +free (RemoveListItem (&(*lists)[undo_type], CmpAny, iid)); }if (msg->header.type == msg_del_dns_cfg)
@@ -1872,40 +1902,43 @@ err =SetDnsSearchDomains (msg->iface.name,NULL , &gpol, lists); }ApplyDnsSettings (gpol); - goto out;/* job done */ +return err;/* job done */ } -for (int i =0 ; i < addr_len; ++i) - { -if (msg->family ==AF_INET6 ) - { -RtlIpv6AddressToStringW (&msg->addr[i].ipv6, addr); - } -else - { -RtlIpv4AddressToStringW (&msg->addr[i].ipv4, addr); - } - err =AddDNS (msg->family, wide_name, addr); -if (i ==0 && err) - { - goto out; - } -/* We do not check for duplicate addresses, so any error in adding -* additional addresses is ignored. -*/ - } - - err =0 ; -if (msg->addr_len >0 ) { -wchar_t *tmp_name =_wcsdup (wide_name); -if (!tmp_name ||AddListItem (&(*lists)[undo_type], tmp_name)) +/* prepare the comma separated address list */ +CHAR addrs[256 ];/* large enough to hold four IPv4 / IPv6 address strings */ +size_t offset =0 ; +for (int i =0 ; i < addr_len; ++i) { -free (tmp_name); -DeleteDNS (msg->family, wide_name); - err =ERROR_OUTOFMEMORY ; - goto out; +if (i !=0 ) + { + addrs[offset++] =',' ; + } +if (msg->family ==AF_INET6 ) + { +RtlIpv6AddressToStringA (&msg->addr[i].ipv6, addrs + offset); + } +else + { +RtlIpv4AddressToStringA (&msg->addr[i].ipv4, addrs + offset); + } + offset +=strlen (addrs); + } + + err =SetNameServers (iid, msg->family, addrs); +if (err) + { +return err; + } + +wchar_t *tmp_iid =_wcsdup (iid); +if (!tmp_iid ||AddListItem (&(*lists)[undo_type], tmp_iid)) + { +free (tmp_iid); +ResetNameServers (iid, msg->family); +return ERROR_OUTOFMEMORY ; } }
@@ -1916,8 +1949,6 @@ }ApplyDnsSettings (gpol); -out: -free (wide_name);return err; }
@@ -2298,11 +2329,11 @@break ;case undo_dns4: -DeleteDNS (AF_INET , item->data); +ResetNameServers (item->data,AF_INET );break ;case undo_dns6: -DeleteDNS (AF_INET6 , item->data); +ResetNameServers (item->data,AF_INET6 );break ;case undo_domains:
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id2bed0908e84c19b8fb6fe806376316793e550b4
Gerrit-Change-Number: 825
Gerrit-PatchSet: 9
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-Reviewer: stipa <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-MessageType: newpatchset
Attention is currently required from: d12fk, flichtenheld, plaisthos.
Hello flichtenheld, plaisthos, stipa,
I'd like you to reexamine a change. Please visit
http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
to look at the new patch set (#10).
The change is no longer submittable: checks~ChecksSubmitRule is unsatisfied now.
Change subject: dns: do not use netsh to set name server addresses
......................................................................
dns: do not use netsh to set name server addresses
Instead of spawning a netsh process, set the name server addresses directly in the registry hive of the VPN interface.
This is a first step to get rid of the use of command line tools in the service and move to a more API driven style of modifying the VPN adapter configuration.
---
M src/openvpnserv/interactive.c
1 file changed, 146 insertions(+), 115 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/25/825/10
src/openvpnserv/interactive.c +146 -115
@@ -1024,65 +1024,6 @@ }/** -* Run the command: netsh interface $proto $action dns $if_name $addr [validate=no] -* @param action "delete" or "add" -* @param proto "ipv6" or "ip" -* @param if_name "name_of_interface" -* @param addr IPv4 (for proto = ip) or IPv6 address as a string -* -* If addr is null and action = "delete" all addresses are deleted. -*/ -static DWORD -netsh_dns_cmd (const wchar_t *action,const wchar_t *proto,const wchar_t *if_name,const wchar_t *addr) -{ -DWORD err =0 ; -int timeout =30000 ;/* in msec */ -wchar_t argv0[MAX_PATH ]; -wchar_t *cmdline =NULL ; - -if (!addr) - { -if (wcscmp (action,L"delete" ) ==0 ) - { - addr =L"all" ; - } -else /* nothing to do -- return success*/ - { - goto out; - } - } - -/* Path of netsh */ -swprintf (argv0,_countof (argv0),L"%ls\\%ls" ,get_win_sys_path (),L"netsh.exe" ); - -/* cmd template: -* netsh interface $proto $action dns $if_name $addr [validate=no] -*/ -const wchar_t *fmt =L"netsh interface %ls %ls dns \"%ls\" %ls" ; - -/* max cmdline length in wchars -- include room for worst case and some */ -size_t ncmdline =wcslen (fmt) +wcslen (if_name) +wcslen (addr) +32 +1 ; - cmdline =malloc (ncmdline*sizeof (wchar_t)); -if (!cmdline) - { - err =ERROR_OUTOFMEMORY ; - goto out; - } - -swprintf (cmdline, ncmdline, fmt, proto, action, if_name, addr); - -if (IsWindows7OrGreater ()) - { -wcscat_s (cmdline, ncmdline,L" validate=no" ); - } - err =ExecCommand (argv0, cmdline, timeout); - -out: -free (cmdline); -return err; -} - -/** * Run the command: netsh interface ip $action wins $if_name [static] $addr * @param action "delete", "add" or "set" * @param if_name "name_of_interface"
@@ -1139,22 +1080,6 @@return err; } -/* Delete all IPv4 or IPv6 dns servers for an interface */ -static DWORD -DeleteDNS (short family,wchar_t *if_name) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"delete" , proto, if_name,NULL ); -} - -/* Add an IPv4 or IPv6 dns server to an interface */ -static DWORD -AddDNS (short family,wchar_t *if_name,wchar_t *addr) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"add" , proto, if_name, addr); -} -static BOOL CmpWString (LPVOID item,LPVOID str) {
@@ -1818,11 +1743,115 @@return err; } +/** +* Return the interfaces registry key for the specified address family +* +* @param family the internet address family to open the key for +* @param key PHKEY to return the key in +* @return BOOL to indicate success or failure +*/ +static BOOL +GetInterfacesKey (short family,PHKEY key) +{ +PCSTR itfs_key = family ==AF_INET6 + ?"SYSTEM\\CurrentControlSet\\Services\\Tcpip6\\Parameters\\Interfaces" + :"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces" ; + +LSTATUS err =RegOpenKeyExA (HKEY_LOCAL_MACHINE , itfs_key,0 ,KEY_ALL_ACCESS , key); +if (err) + { + *key =INVALID_HANDLE_VALUE ; +MsgToEventLog (M_SYSERR ,TEXT ("GetInterfacesKey: " +"could not open interfaces registry key for family %d (%lu)" ), + family, err); + } + +return err ? FALSE : TRUE; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param value the value to set the name servers to +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServersValue (PCWSTR itf_id,short family,PCSTR value) +{ +DWORD err; + +HKEY itfs; +if (!GetInterfacesKey (family, &itfs)) + { +return ERROR_FILE_NOT_FOUND ; + } + +HKEY itf =INVALID_HANDLE_VALUE ; + err =RegOpenKeyExW (itfs, itf_id,0 ,KEY_ALL_ACCESS , &itf); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not open interface key for %s family %d (%lu)" ), + itf_id, family, err); + goto out; + } + + err =RegSetValueExA (itf,"NameServer" ,0 ,REG_SZ , (PBYTE )value,strlen (value) +1 ); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not set name servers '%S' for %s family %d (%lu)" ), + value, itf_id, family, err); + } + +out: +if (itf !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itf); + } +if (itfs !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itfs); + } +return err; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param addrs comma separated list of name server addresses +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServers (PCWSTR itf_id,short family,PCSTR addrs) +{ +return SetNameServersValue (itf_id, family, addrs); +} + +/** +* Delete all DNS name servers from a registry interface configuration +* +* @param itf_id the interface id to clear the servers for +* @param family internet address family to clear the servers for +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +ResetNameServers (PCWSTR itf_id,short family) +{ +return SetNameServersValue (itf_id, family,"" ); +} +static DWORD HandleDNSConfigMessage (const dns_cfg_message_t *msg,undo_lists_t *lists) {DWORD err =0 ; -wchar_t addr[46 ];/* large enough to hold string representation of an ipv4 / ipv6 address */ undo_type_t undo_type = (msg->family ==AF_INET6 ) ? undo_dns4 : undo_dns6;int addr_len = msg->addr_len;
@@ -1844,10 +1873,11 @@ msgptr->domains[_countof (msg->domains)-1 ] ='\0' ; } -wchar_t *wide_name =utf8to16 (msg->iface.name);/* utf8 to wide-char */ -if (!wide_name) +WCHAR iid[64 ]; + err =InterfaceIdString (msg->iface.name, iid,_countof (iid)); +if (err) { -return ERROR_OUTOFMEMORY ; +return err; }/* We delete all current addresses before adding any
@@ -1855,12 +1885,12 @@*/ if (addr_len >0 || msg->header.type == msg_del_dns_cfg) { - err =DeleteDNS (msg->family, wide_name); + err =ResetNameServers (iid, msg->family);if (err) { - goto out; +return err; } -free (RemoveListItem (&(*lists)[undo_type], CmpWString, wide_name)); +free (RemoveListItem (&(*lists)[undo_type], CmpAny, iid)); }if (msg->header.type == msg_del_dns_cfg)
@@ -1872,40 +1902,43 @@ err =SetDnsSearchDomains (msg->iface.name,NULL , &gpol, lists); }ApplyDnsSettings (gpol); - goto out;/* job done */ +return err;/* job done */ } -for (int i =0 ; i < addr_len; ++i) - { -if (msg->family ==AF_INET6 ) - { -RtlIpv6AddressToStringW (&msg->addr[i].ipv6, addr); - } -else - { -RtlIpv4AddressToStringW (&msg->addr[i].ipv4, addr); - } - err =AddDNS (msg->family, wide_name, addr); -if (i ==0 && err) - { - goto out; - } -/* We do not check for duplicate addresses, so any error in adding -* additional addresses is ignored. -*/ - } - - err =0 ; -if (msg->addr_len >0 ) { -wchar_t *tmp_name =_wcsdup (wide_name); -if (!tmp_name ||AddListItem (&(*lists)[undo_type], tmp_name)) +/* prepare the comma separated address list */ +CHAR addrs[256 ];/* large enough to hold four IPv4 / IPv6 address strings */ +size_t offset =0 ; +for (int i =0 ; i < addr_len; ++i) { -free (tmp_name); -DeleteDNS (msg->family, wide_name); - err =ERROR_OUTOFMEMORY ; - goto out; +if (i !=0 ) + { + addrs[offset++] =',' ; + } +if (msg->family ==AF_INET6 ) + { +RtlIpv6AddressToStringA (&msg->addr[i].ipv6, addrs + offset); + } +else + { +RtlIpv4AddressToStringA (&msg->addr[i].ipv4, addrs + offset); + } + offset +=strlen (addrs); + } + + err =SetNameServers (iid, msg->family, addrs); +if (err) + { +return err; + } + +wchar_t *tmp_iid =_wcsdup (iid); +if (!tmp_iid ||AddListItem (&(*lists)[undo_type], tmp_iid)) + { +free (tmp_iid); +ResetNameServers (iid, msg->family); +return ERROR_OUTOFMEMORY ; } }
@@ -1916,8 +1949,6 @@ }ApplyDnsSettings (gpol); -out: -free (wide_name);return err; }
@@ -2298,11 +2329,11 @@break ;case undo_dns4: -DeleteDNS (AF_INET , item->data); +ResetNameServers (item->data,AF_INET );break ;case undo_dns6: -DeleteDNS (AF_INET6 , item->data); +ResetNameServers (item->data,AF_INET6 );break ;case undo_domains:
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id2bed0908e84c19b8fb6fe806376316793e550b4
Gerrit-Change-Number: 825
Gerrit-PatchSet: 10
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-Reviewer: stipa <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-MessageType: newpatchset
Attention is currently required from: d12fk, flichtenheld, plaisthos.
Hello flichtenheld, plaisthos, stipa,
I'd like you to reexamine a change. Please visit
http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
to look at the new patch set (#11).
The change is no longer submittable: checks~ChecksSubmitRule is unsatisfied now.
Change subject: dns: do not use netsh to set name server addresses
......................................................................
dns: do not use netsh to set name server addresses
Instead of spawning a netsh process, set the name server addresses directly in the registry hive of the VPN interface.
This is a first step to get rid of the use of command line tools in the service and move to a more API driven style of modifying the VPN adapter configuration.
---
M src/openvpnserv/interactive.c
1 file changed, 146 insertions(+), 115 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/25/825/11
src/openvpnserv/interactive.c +146 -115
@@ -1024,65 +1024,6 @@ }/** -* Run the command: netsh interface $proto $action dns $if_name $addr [validate=no] -* @param action "delete" or "add" -* @param proto "ipv6" or "ip" -* @param if_name "name_of_interface" -* @param addr IPv4 (for proto = ip) or IPv6 address as a string -* -* If addr is null and action = "delete" all addresses are deleted. -*/ -static DWORD -netsh_dns_cmd (const wchar_t *action,const wchar_t *proto,const wchar_t *if_name,const wchar_t *addr) -{ -DWORD err =0 ; -int timeout =30000 ;/* in msec */ -wchar_t argv0[MAX_PATH ]; -wchar_t *cmdline =NULL ; - -if (!addr) - { -if (wcscmp (action,L"delete" ) ==0 ) - { - addr =L"all" ; - } -else /* nothing to do -- return success*/ - { - goto out; - } - } - -/* Path of netsh */ -swprintf (argv0,_countof (argv0),L"%ls\\%ls" ,get_win_sys_path (),L"netsh.exe" ); - -/* cmd template: -* netsh interface $proto $action dns $if_name $addr [validate=no] -*/ -const wchar_t *fmt =L"netsh interface %ls %ls dns \"%ls\" %ls" ; - -/* max cmdline length in wchars -- include room for worst case and some */ -size_t ncmdline =wcslen (fmt) +wcslen (if_name) +wcslen (addr) +32 +1 ; - cmdline =malloc (ncmdline*sizeof (wchar_t)); -if (!cmdline) - { - err =ERROR_OUTOFMEMORY ; - goto out; - } - -swprintf (cmdline, ncmdline, fmt, proto, action, if_name, addr); - -if (IsWindows7OrGreater ()) - { -wcscat_s (cmdline, ncmdline,L" validate=no" ); - } - err =ExecCommand (argv0, cmdline, timeout); - -out: -free (cmdline); -return err; -} - -/** * Run the command: netsh interface ip $action wins $if_name [static] $addr * @param action "delete", "add" or "set" * @param if_name "name_of_interface"
@@ -1139,22 +1080,6 @@return err; } -/* Delete all IPv4 or IPv6 dns servers for an interface */ -static DWORD -DeleteDNS (short family,wchar_t *if_name) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"delete" , proto, if_name,NULL ); -} - -/* Add an IPv4 or IPv6 dns server to an interface */ -static DWORD -AddDNS (short family,wchar_t *if_name,wchar_t *addr) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"add" , proto, if_name, addr); -} -static BOOL CmpWString (LPVOID item,LPVOID str) {
@@ -1818,11 +1743,115 @@return err; } +/** +* Return the interfaces registry key for the specified address family +* +* @param family the internet address family to open the key for +* @param key PHKEY to return the key in +* @return BOOL to indicate success or failure +*/ +static BOOL +GetInterfacesKey (short family,PHKEY key) +{ +PCSTR itfs_key = family ==AF_INET6 + ?"SYSTEM\\CurrentControlSet\\Services\\Tcpip6\\Parameters\\Interfaces" + :"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces" ; + +LSTATUS err =RegOpenKeyExA (HKEY_LOCAL_MACHINE , itfs_key,0 ,KEY_ALL_ACCESS , key); +if (err) + { + *key =INVALID_HANDLE_VALUE ; +MsgToEventLog (M_SYSERR ,TEXT ("GetInterfacesKey: " +"could not open interfaces registry key for family %d (%lu)" ), + family, err); + } + +return err ? FALSE : TRUE; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param value the value to set the name servers to +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServersValue (PCWSTR itf_id,short family,PCSTR value) +{ +DWORD err; + +HKEY itfs; +if (!GetInterfacesKey (family, &itfs)) + { +return ERROR_FILE_NOT_FOUND ; + } + +HKEY itf =INVALID_HANDLE_VALUE ; + err =RegOpenKeyExW (itfs, itf_id,0 ,KEY_ALL_ACCESS , &itf); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not open interface key for %s family %d (%lu)" ), + itf_id, family, err); + goto out; + } + + err =RegSetValueExA (itf,"NameServer" ,0 ,REG_SZ , (PBYTE )value,strlen (value) +1 ); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not set name servers '%S' for %s family %d (%lu)" ), + value, itf_id, family, err); + } + +out: +if (itf !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itf); + } +if (itfs !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itfs); + } +return err; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param addrs comma separated list of name server addresses +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServers (PCWSTR itf_id,short family,PCSTR addrs) +{ +return SetNameServersValue (itf_id, family, addrs); +} + +/** +* Delete all DNS name servers from a registry interface configuration +* +* @param itf_id the interface id to clear the servers for +* @param family internet address family to clear the servers for +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +ResetNameServers (PCWSTR itf_id,short family) +{ +return SetNameServersValue (itf_id, family,"" ); +} +static DWORD HandleDNSConfigMessage (const dns_cfg_message_t *msg,undo_lists_t *lists) {DWORD err =0 ; -wchar_t addr[46 ];/* large enough to hold string representation of an ipv4 / ipv6 address */ undo_type_t undo_type = (msg->family ==AF_INET6 ) ? undo_dns4 : undo_dns6;int addr_len = msg->addr_len;
@@ -1844,10 +1873,11 @@ msgptr->domains[_countof (msg->domains)-1 ] ='\0' ; } -wchar_t *wide_name =utf8to16 (msg->iface.name);/* utf8 to wide-char */ -if (!wide_name) +WCHAR iid[64 ]; + err =InterfaceIdString (msg->iface.name, iid,_countof (iid)); +if (err) { -return ERROR_OUTOFMEMORY ; +return err; }/* We delete all current addresses before adding any
@@ -1855,12 +1885,12 @@*/ if (addr_len >0 || msg->header.type == msg_del_dns_cfg) { - err =DeleteDNS (msg->family, wide_name); + err =ResetNameServers (iid, msg->family);if (err) { - goto out; +return err; } -free (RemoveListItem (&(*lists)[undo_type], CmpWString, wide_name)); +free (RemoveListItem (&(*lists)[undo_type], CmpAny, iid)); }if (msg->header.type == msg_del_dns_cfg)
@@ -1872,40 +1902,43 @@ err =SetDnsSearchDomains (msg->iface.name,NULL , &gpol, lists); }ApplyDnsSettings (gpol); - goto out;/* job done */ +return err;/* job done */ } -for (int i =0 ; i < addr_len; ++i) - { -if (msg->family ==AF_INET6 ) - { -RtlIpv6AddressToStringW (&msg->addr[i].ipv6, addr); - } -else - { -RtlIpv4AddressToStringW (&msg->addr[i].ipv4, addr); - } - err =AddDNS (msg->family, wide_name, addr); -if (i ==0 && err) - { - goto out; - } -/* We do not check for duplicate addresses, so any error in adding -* additional addresses is ignored. -*/ - } - - err =0 ; -if (msg->addr_len >0 ) { -wchar_t *tmp_name =_wcsdup (wide_name); -if (!tmp_name ||AddListItem (&(*lists)[undo_type], tmp_name)) +/* prepare the comma separated address list */ +CHAR addrs[256 ];/* large enough to hold four IPv4 / IPv6 address strings */ +size_t offset =0 ; +for (int i =0 ; i < addr_len; ++i) { -free (tmp_name); -DeleteDNS (msg->family, wide_name); - err =ERROR_OUTOFMEMORY ; - goto out; +if (i !=0 ) + { + addrs[offset++] =',' ; + } +if (msg->family ==AF_INET6 ) + { +RtlIpv6AddressToStringA (&msg->addr[i].ipv6, addrs + offset); + } +else + { +RtlIpv4AddressToStringA (&msg->addr[i].ipv4, addrs + offset); + } + offset +=strlen (addrs); + } + + err =SetNameServers (iid, msg->family, addrs); +if (err) + { +return err; + } + +wchar_t *tmp_iid =_wcsdup (iid); +if (!tmp_iid ||AddListItem (&(*lists)[undo_type], tmp_iid)) + { +free (tmp_iid); +ResetNameServers (iid, msg->family); +return ERROR_OUTOFMEMORY ; } }
@@ -1916,8 +1949,6 @@ }ApplyDnsSettings (gpol); -out: -free (wide_name);return err; }
@@ -2298,11 +2329,11 @@break ;case undo_dns4: -DeleteDNS (AF_INET , item->data); +ResetNameServers (item->data,AF_INET );break ;case undo_dns6: -DeleteDNS (AF_INET6 , item->data); +ResetNameServers (item->data,AF_INET6 );break ;case undo_domains:
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id2bed0908e84c19b8fb6fe806376316793e550b4
Gerrit-Change-Number: 825
Gerrit-PatchSet: 11
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-Reviewer: stipa <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-MessageType: newpatchset
Attention is currently required from: d12fk, flichtenheld, plaisthos.
Hello flichtenheld, plaisthos, stipa,
I'd like you to reexamine a change. Please visit
http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
to look at the new patch set (#12).
The change is no longer submittable: checks~ChecksSubmitRule is unsatisfied now.
Change subject: dns: do not use netsh to set name server addresses
......................................................................
dns: do not use netsh to set name server addresses
Instead of spawning a netsh process, set the name server addresses directly in the registry hive of the VPN interface.
This is a first step to get rid of the use of command line tools in the service and move to a more API driven style of modifying the VPN adapter configuration.
---
M src/openvpnserv/interactive.c
1 file changed, 146 insertions(+), 115 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/25/825/12
src/openvpnserv/interactive.c +146 -115
@@ -1024,65 +1024,6 @@ }/** -* Run the command: netsh interface $proto $action dns $if_name $addr [validate=no] -* @param action "delete" or "add" -* @param proto "ipv6" or "ip" -* @param if_name "name_of_interface" -* @param addr IPv4 (for proto = ip) or IPv6 address as a string -* -* If addr is null and action = "delete" all addresses are deleted. -*/ -static DWORD -netsh_dns_cmd (const wchar_t *action,const wchar_t *proto,const wchar_t *if_name,const wchar_t *addr) -{ -DWORD err =0 ; -int timeout =30000 ;/* in msec */ -wchar_t argv0[MAX_PATH ]; -wchar_t *cmdline =NULL ; - -if (!addr) - { -if (wcscmp (action,L"delete" ) ==0 ) - { - addr =L"all" ; - } -else /* nothing to do -- return success*/ - { - goto out; - } - } - -/* Path of netsh */ -swprintf (argv0,_countof (argv0),L"%ls\\%ls" ,get_win_sys_path (),L"netsh.exe" ); - -/* cmd template: -* netsh interface $proto $action dns $if_name $addr [validate=no] -*/ -const wchar_t *fmt =L"netsh interface %ls %ls dns \"%ls\" %ls" ; - -/* max cmdline length in wchars -- include room for worst case and some */ -size_t ncmdline =wcslen (fmt) +wcslen (if_name) +wcslen (addr) +32 +1 ; - cmdline =malloc (ncmdline*sizeof (wchar_t)); -if (!cmdline) - { - err =ERROR_OUTOFMEMORY ; - goto out; - } - -swprintf (cmdline, ncmdline, fmt, proto, action, if_name, addr); - -if (IsWindows7OrGreater ()) - { -wcscat_s (cmdline, ncmdline,L" validate=no" ); - } - err =ExecCommand (argv0, cmdline, timeout); - -out: -free (cmdline); -return err; -} - -/** * Run the command: netsh interface ip $action wins $if_name [static] $addr * @param action "delete", "add" or "set" * @param if_name "name_of_interface"
@@ -1139,22 +1080,6 @@return err; } -/* Delete all IPv4 or IPv6 dns servers for an interface */ -static DWORD -DeleteDNS (short family,wchar_t *if_name) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"delete" , proto, if_name,NULL ); -} - -/* Add an IPv4 or IPv6 dns server to an interface */ -static DWORD -AddDNS (short family,wchar_t *if_name,wchar_t *addr) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"add" , proto, if_name, addr); -} -static BOOL CmpWString (LPVOID item,LPVOID str) {
@@ -1818,11 +1743,115 @@return err; } +/** +* Return the interfaces registry key for the specified address family +* +* @param family the internet address family to open the key for +* @param key PHKEY to return the key in +* @return BOOL to indicate success or failure +*/ +static BOOL +GetInterfacesKey (short family,PHKEY key) +{ +PCSTR itfs_key = family ==AF_INET6 + ?"SYSTEM\\CurrentControlSet\\Services\\Tcpip6\\Parameters\\Interfaces" + :"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces" ; + +LSTATUS err =RegOpenKeyExA (HKEY_LOCAL_MACHINE , itfs_key,0 ,KEY_ALL_ACCESS , key); +if (err) + { + *key =INVALID_HANDLE_VALUE ; +MsgToEventLog (M_SYSERR ,TEXT ("GetInterfacesKey: " +"could not open interfaces registry key for family %d (%lu)" ), + family, err); + } + +return err ? FALSE : TRUE; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param value the value to set the name servers to +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServersValue (PCWSTR itf_id,short family,PCSTR value) +{ +DWORD err; + +HKEY itfs; +if (!GetInterfacesKey (family, &itfs)) + { +return ERROR_FILE_NOT_FOUND ; + } + +HKEY itf =INVALID_HANDLE_VALUE ; + err =RegOpenKeyExW (itfs, itf_id,0 ,KEY_ALL_ACCESS , &itf); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not open interface key for %s family %d (%lu)" ), + itf_id, family, err); + goto out; + } + + err =RegSetValueExA (itf,"NameServer" ,0 ,REG_SZ , (PBYTE )value,strlen (value) +1 ); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not set name servers '%S' for %s family %d (%lu)" ), + value, itf_id, family, err); + } + +out: +if (itf !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itf); + } +if (itfs !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itfs); + } +return err; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param addrs comma separated list of name server addresses +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServers (PCWSTR itf_id,short family,PCSTR addrs) +{ +return SetNameServersValue (itf_id, family, addrs); +} + +/** +* Delete all DNS name servers from a registry interface configuration +* +* @param itf_id the interface id to clear the servers for +* @param family internet address family to clear the servers for +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +ResetNameServers (PCWSTR itf_id,short family) +{ +return SetNameServersValue (itf_id, family,"" ); +} +static DWORD HandleDNSConfigMessage (const dns_cfg_message_t *msg,undo_lists_t *lists) {DWORD err =0 ; -wchar_t addr[46 ];/* large enough to hold string representation of an ipv4 / ipv6 address */ undo_type_t undo_type = (msg->family ==AF_INET6 ) ? undo_dns4 : undo_dns6;int addr_len = msg->addr_len;
@@ -1844,10 +1873,11 @@ msgptr->domains[_countof (msg->domains)-1 ] ='\0' ; } -wchar_t *wide_name =utf8to16 (msg->iface.name);/* utf8 to wide-char */ -if (!wide_name) +WCHAR iid[64 ]; + err =InterfaceIdString (msg->iface.name, iid,_countof (iid)); +if (err) { -return ERROR_OUTOFMEMORY ; +return err; }/* We delete all current addresses before adding any
@@ -1855,12 +1885,12 @@*/ if (addr_len >0 || msg->header.type == msg_del_dns_cfg) { - err =DeleteDNS (msg->family, wide_name); + err =ResetNameServers (iid, msg->family);if (err) { - goto out; +return err; } -free (RemoveListItem (&(*lists)[undo_type], CmpWString, wide_name)); +free (RemoveListItem (&(*lists)[undo_type], CmpAny, iid)); }if (msg->header.type == msg_del_dns_cfg)
@@ -1872,40 +1902,43 @@ err =SetDnsSearchDomains (msg->iface.name,NULL , &gpol, lists); }ApplyDnsSettings (gpol); - goto out;/* job done */ +return err;/* job done */ } -for (int i =0 ; i < addr_len; ++i) - { -if (msg->family ==AF_INET6 ) - { -RtlIpv6AddressToStringW (&msg->addr[i].ipv6, addr); - } -else - { -RtlIpv4AddressToStringW (&msg->addr[i].ipv4, addr); - } - err =AddDNS (msg->family, wide_name, addr); -if (i ==0 && err) - { - goto out; - } -/* We do not check for duplicate addresses, so any error in adding -* additional addresses is ignored. -*/ - } - - err =0 ; -if (msg->addr_len >0 ) { -wchar_t *tmp_name =_wcsdup (wide_name); -if (!tmp_name ||AddListItem (&(*lists)[undo_type], tmp_name)) +/* prepare the comma separated address list */ +CHAR addrs[256 ];/* large enough to hold four IPv4 / IPv6 address strings */ +size_t offset =0 ; +for (int i =0 ; i < addr_len; ++i) { -free (tmp_name); -DeleteDNS (msg->family, wide_name); - err =ERROR_OUTOFMEMORY ; - goto out; +if (i !=0 ) + { + addrs[offset++] =',' ; + } +if (msg->family ==AF_INET6 ) + { +RtlIpv6AddressToStringA (&msg->addr[i].ipv6, addrs + offset); + } +else + { +RtlIpv4AddressToStringA (&msg->addr[i].ipv4, addrs + offset); + } + offset +=strlen (addrs); + } + + err =SetNameServers (iid, msg->family, addrs); +if (err) + { +return err; + } + +wchar_t *tmp_iid =_wcsdup (iid); +if (!tmp_iid ||AddListItem (&(*lists)[undo_type], tmp_iid)) + { +free (tmp_iid); +ResetNameServers (iid, msg->family); +return ERROR_OUTOFMEMORY ; } }
@@ -1916,8 +1949,6 @@ }ApplyDnsSettings (gpol); -out: -free (wide_name);return err; }
@@ -2298,11 +2329,11 @@break ;case undo_dns4: -DeleteDNS (AF_INET , item->data); +ResetNameServers (item->data,AF_INET );break ;case undo_dns6: -DeleteDNS (AF_INET6 , item->data); +ResetNameServers (item->data,AF_INET6 );break ;case undo_domains:
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id2bed0908e84c19b8fb6fe806376316793e550b4
Gerrit-Change-Number: 825
Gerrit-PatchSet: 12
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-Reviewer: stipa <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-MessageType: newpatchset
PATCH v12 1/1 dns: do not use netsh to set name server addresses
In reply to a message not in the archiveFrom: Heiko Hund <[email protected]...>
Instead of spawning a netsh process, set the name server addresses directly in the registry hive of the VPN interface.
This is a first step to get rid of the use of command line tools in the service and move to a more API driven style of modifying the VPN adapter configuration.
---
This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master.
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/825
This mail reflects revision 12 of this Change.
Acked-by according to Gerrit (reflected above):
Lev Stipakov <[email protected]...>
src/openvpnserv/interactive.c +146 -115
@@ -1024,65 +1024,6 @@ }/** -* Run the command: netsh interface $proto $action dns $if_name $addr [validate=no] -* @param action "delete" or "add" -* @param proto "ipv6" or "ip" -* @param if_name "name_of_interface" -* @param addr IPv4 (for proto = ip) or IPv6 address as a string -* -* If addr is null and action = "delete" all addresses are deleted. -*/ -static DWORD -netsh_dns_cmd (const wchar_t *action,const wchar_t *proto,const wchar_t *if_name,const wchar_t *addr) -{ -DWORD err =0 ; -int timeout =30000 ;/* in msec */ -wchar_t argv0[MAX_PATH ]; -wchar_t *cmdline =NULL ; - -if (!addr) - { -if (wcscmp (action,L"delete" ) ==0 ) - { - addr =L"all" ; - } -else /* nothing to do -- return success*/ - { - goto out; - } - } - -/* Path of netsh */ -swprintf (argv0,_countof (argv0),L"%ls\\%ls" ,get_win_sys_path (),L"netsh.exe" ); - -/* cmd template: -* netsh interface $proto $action dns $if_name $addr [validate=no] -*/ -const wchar_t *fmt =L"netsh interface %ls %ls dns \"%ls\" %ls" ; - -/* max cmdline length in wchars -- include room for worst case and some */ -size_t ncmdline =wcslen (fmt) +wcslen (if_name) +wcslen (addr) +32 +1 ; - cmdline =malloc (ncmdline*sizeof (wchar_t)); -if (!cmdline) - { - err =ERROR_OUTOFMEMORY ; - goto out; - } - -swprintf (cmdline, ncmdline, fmt, proto, action, if_name, addr); - -if (IsWindows7OrGreater ()) - { -wcscat_s (cmdline, ncmdline,L" validate=no" ); - } - err =ExecCommand (argv0, cmdline, timeout); - -out: -free (cmdline); -return err; -} - -/** * Run the command: netsh interface ip $action wins $if_name [static] $addr * @param action "delete", "add" or "set" * @param if_name "name_of_interface"
@@ -1139,22 +1080,6 @@return err; } -/* Delete all IPv4 or IPv6 dns servers for an interface */ -static DWORD -DeleteDNS (short family,wchar_t *if_name) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"delete" , proto, if_name,NULL ); -} - -/* Add an IPv4 or IPv6 dns server to an interface */ -static DWORD -AddDNS (short family,wchar_t *if_name,wchar_t *addr) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"add" , proto, if_name, addr); -} -static BOOL CmpWString (LPVOID item,LPVOID str) {
@@ -1818,11 +1743,115 @@return err; } +/** +* Return the interfaces registry key for the specified address family +* +* @param family the internet address family to open the key for +* @param key PHKEY to return the key in +* @return BOOL to indicate success or failure +*/ +static BOOL +GetInterfacesKey (short family,PHKEY key) +{ +PCSTR itfs_key = family ==AF_INET6 + ?"SYSTEM\\CurrentControlSet\\Services\\Tcpip6\\Parameters\\Interfaces" + :"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces" ; + +LSTATUS err =RegOpenKeyExA (HKEY_LOCAL_MACHINE , itfs_key,0 ,KEY_ALL_ACCESS , key); +if (err) + { + *key =INVALID_HANDLE_VALUE ; +MsgToEventLog (M_SYSERR ,TEXT ("GetInterfacesKey: " +"could not open interfaces registry key for family %d (%lu)" ), + family, err); + } + +return err ? FALSE : TRUE; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param value the value to set the name servers to +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServersValue (PCWSTR itf_id,short family,PCSTR value) +{ +DWORD err; + +HKEY itfs; +if (!GetInterfacesKey (family, &itfs)) + { +return ERROR_FILE_NOT_FOUND ; + } + +HKEY itf =INVALID_HANDLE_VALUE ; + err =RegOpenKeyExW (itfs, itf_id,0 ,KEY_ALL_ACCESS , &itf); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not open interface key for %s family %d (%lu)" ), + itf_id, family, err); + goto out; + } + + err =RegSetValueExA (itf,"NameServer" ,0 ,REG_SZ , (PBYTE )value,strlen (value) +1 ); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not set name servers '%S' for %s family %d (%lu)" ), + value, itf_id, family, err); + } + +out: +if (itf !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itf); + } +if (itfs !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itfs); + } +return err; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param addrs comma separated list of name server addresses +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServers (PCWSTR itf_id,short family,PCSTR addrs) +{ +return SetNameServersValue (itf_id, family, addrs); +} + +/** +* Delete all DNS name servers from a registry interface configuration +* +* @param itf_id the interface id to clear the servers for +* @param family internet address family to clear the servers for +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +ResetNameServers (PCWSTR itf_id,short family) +{ +return SetNameServersValue (itf_id, family,"" ); +} +static DWORD HandleDNSConfigMessage (const dns_cfg_message_t *msg,undo_lists_t *lists) {DWORD err =0 ; -wchar_t addr[46 ];/* large enough to hold string representation of an ipv4 / ipv6 address */ undo_type_t undo_type = (msg->family ==AF_INET6 ) ? undo_dns4 : undo_dns6;int addr_len = msg->addr_len;
@@ -1844,10 +1873,11 @@ msgptr->domains[_countof (msg->domains)-1 ] ='\0' ; } -wchar_t *wide_name =utf8to16 (msg->iface.name);/* utf8 to wide-char */ -if (!wide_name) +WCHAR iid[64 ]; + err =InterfaceIdString (msg->iface.name, iid,_countof (iid)); +if (err) { -return ERROR_OUTOFMEMORY ; +return err; }/* We delete all current addresses before adding any
@@ -1855,12 +1885,12 @@*/ if (addr_len >0 || msg->header.type == msg_del_dns_cfg) { - err =DeleteDNS (msg->family, wide_name); + err =ResetNameServers (iid, msg->family);if (err) { - goto out; +return err; } -free (RemoveListItem (&(*lists)[undo_type], CmpWString, wide_name)); +free (RemoveListItem (&(*lists)[undo_type], CmpAny, iid)); }if (msg->header.type == msg_del_dns_cfg)
@@ -1872,40 +1902,43 @@ err =SetDnsSearchDomains (msg->iface.name,NULL , &gpol, lists); }ApplyDnsSettings (gpol); - goto out;/* job done */ +return err;/* job done */ } -for (int i =0 ; i < addr_len; ++i) - { -if (msg->family ==AF_INET6 ) - { -RtlIpv6AddressToStringW (&msg->addr[i].ipv6, addr); - } -else - { -RtlIpv4AddressToStringW (&msg->addr[i].ipv4, addr); - } - err =AddDNS (msg->family, wide_name, addr); -if (i ==0 && err) - { - goto out; - } -/* We do not check for duplicate addresses, so any error in adding -* additional addresses is ignored. -*/ - } - - err =0 ; -if (msg->addr_len >0 ) { -wchar_t *tmp_name =_wcsdup (wide_name); -if (!tmp_name ||AddListItem (&(*lists)[undo_type], tmp_name)) +/* prepare the comma separated address list */ +CHAR addrs[256 ];/* large enough to hold four IPv4 / IPv6 address strings */ +size_t offset =0 ; +for (int i =0 ; i < addr_len; ++i) { -free (tmp_name); -DeleteDNS (msg->family, wide_name); - err =ERROR_OUTOFMEMORY ; - goto out; +if (i !=0 ) + { + addrs[offset++] =',' ; + } +if (msg->family ==AF_INET6 ) + { +RtlIpv6AddressToStringA (&msg->addr[i].ipv6, addrs + offset); + } +else + { +RtlIpv4AddressToStringA (&msg->addr[i].ipv4, addrs + offset); + } + offset +=strlen (addrs); + } + + err =SetNameServers (iid, msg->family, addrs); +if (err) + { +return err; + } + +wchar_t *tmp_iid =_wcsdup (iid); +if (!tmp_iid ||AddListItem (&(*lists)[undo_type], tmp_iid)) + { +free (tmp_iid); +ResetNameServers (iid, msg->family); +return ERROR_OUTOFMEMORY ; } }
@@ -1916,8 +1949,6 @@ }ApplyDnsSettings (gpol); -out: -free (wide_name);return err; }
@@ -2298,11 +2329,11 @@break ;case undo_dns4: -DeleteDNS (AF_INET , item->data); +ResetNameServers (item->data,AF_INET );break ;case undo_dns6: -DeleteDNS (AF_INET6 , item->data); +ResetNameServers (item->data,AF_INET6 );break ;case undo_domains:
Attention is currently required from: d12fk, flichtenheld, plaisthos.
Hello flichtenheld, plaisthos, stipa,
I'd like you to reexamine a change. Please visit
http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
to look at the new patch set (#13).
The change is no longer submittable: checks~ChecksSubmitRule is unsatisfied now.
Change subject: dns: do not use netsh to set name server addresses
......................................................................
dns: do not use netsh to set name server addresses
Instead of spawning a netsh process, set the name server addresses directly in the registry hive of the VPN interface.
This is a first step to get rid of the use of command line tools in the service and move to a more API driven style of modifying the VPN adapter configuration.
---
M src/openvpnserv/interactive.c
1 file changed, 146 insertions(+), 115 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/25/825/13
src/openvpnserv/interactive.c +146 -115
@@ -1024,65 +1024,6 @@ }/** -* Run the command: netsh interface $proto $action dns $if_name $addr [validate=no] -* @param action "delete" or "add" -* @param proto "ipv6" or "ip" -* @param if_name "name_of_interface" -* @param addr IPv4 (for proto = ip) or IPv6 address as a string -* -* If addr is null and action = "delete" all addresses are deleted. -*/ -static DWORD -netsh_dns_cmd (const wchar_t *action,const wchar_t *proto,const wchar_t *if_name,const wchar_t *addr) -{ -DWORD err =0 ; -int timeout =30000 ;/* in msec */ -wchar_t argv0[MAX_PATH ]; -wchar_t *cmdline =NULL ; - -if (!addr) - { -if (wcscmp (action,L"delete" ) ==0 ) - { - addr =L"all" ; - } -else /* nothing to do -- return success*/ - { - goto out; - } - } - -/* Path of netsh */ -swprintf (argv0,_countof (argv0),L"%ls\\%ls" ,get_win_sys_path (),L"netsh.exe" ); - -/* cmd template: -* netsh interface $proto $action dns $if_name $addr [validate=no] -*/ -const wchar_t *fmt =L"netsh interface %ls %ls dns \"%ls\" %ls" ; - -/* max cmdline length in wchars -- include room for worst case and some */ -size_t ncmdline =wcslen (fmt) +wcslen (if_name) +wcslen (addr) +32 +1 ; - cmdline =malloc (ncmdline*sizeof (wchar_t)); -if (!cmdline) - { - err =ERROR_OUTOFMEMORY ; - goto out; - } - -swprintf (cmdline, ncmdline, fmt, proto, action, if_name, addr); - -if (IsWindows7OrGreater ()) - { -wcscat_s (cmdline, ncmdline,L" validate=no" ); - } - err =ExecCommand (argv0, cmdline, timeout); - -out: -free (cmdline); -return err; -} - -/** * Run the command: netsh interface ip $action wins $if_name [static] $addr * @param action "delete", "add" or "set" * @param if_name "name_of_interface"
@@ -1139,22 +1080,6 @@return err; } -/* Delete all IPv4 or IPv6 dns servers for an interface */ -static DWORD -DeleteDNS (short family,wchar_t *if_name) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"delete" , proto, if_name,NULL ); -} - -/* Add an IPv4 or IPv6 dns server to an interface */ -static DWORD -AddDNS (short family,wchar_t *if_name,wchar_t *addr) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"add" , proto, if_name, addr); -} -static BOOL CmpWString (LPVOID item,LPVOID str) {
@@ -1819,11 +1744,115 @@return err; } +/** +* Return the interfaces registry key for the specified address family +* +* @param family the internet address family to open the key for +* @param key PHKEY to return the key in +* @return BOOL to indicate success or failure +*/ +static BOOL +GetInterfacesKey (short family,PHKEY key) +{ +PCSTR itfs_key = family ==AF_INET6 + ?"SYSTEM\\CurrentControlSet\\Services\\Tcpip6\\Parameters\\Interfaces" + :"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces" ; + +LSTATUS err =RegOpenKeyExA (HKEY_LOCAL_MACHINE , itfs_key,0 ,KEY_ALL_ACCESS , key); +if (err) + { + *key =INVALID_HANDLE_VALUE ; +MsgToEventLog (M_SYSERR ,TEXT ("GetInterfacesKey: " +"could not open interfaces registry key for family %d (%lu)" ), + family, err); + } + +return err ? FALSE : TRUE; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param value the value to set the name servers to +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServersValue (PCWSTR itf_id,short family,PCSTR value) +{ +DWORD err; + +HKEY itfs; +if (!GetInterfacesKey (family, &itfs)) + { +return ERROR_FILE_NOT_FOUND ; + } + +HKEY itf =INVALID_HANDLE_VALUE ; + err =RegOpenKeyExW (itfs, itf_id,0 ,KEY_ALL_ACCESS , &itf); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not open interface key for %s family %d (%lu)" ), + itf_id, family, err); + goto out; + } + + err =RegSetValueExA (itf,"NameServer" ,0 ,REG_SZ , (PBYTE )value,strlen (value) +1 ); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not set name servers '%S' for %s family %d (%lu)" ), + value, itf_id, family, err); + } + +out: +if (itf !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itf); + } +if (itfs !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itfs); + } +return err; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param addrs comma separated list of name server addresses +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServers (PCWSTR itf_id,short family,PCSTR addrs) +{ +return SetNameServersValue (itf_id, family, addrs); +} + +/** +* Delete all DNS name servers from a registry interface configuration +* +* @param itf_id the interface id to clear the servers for +* @param family internet address family to clear the servers for +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +ResetNameServers (PCWSTR itf_id,short family) +{ +return SetNameServersValue (itf_id, family,"" ); +} +static DWORD HandleDNSConfigMessage (const dns_cfg_message_t *msg,undo_lists_t *lists) {DWORD err =0 ; -wchar_t addr[46 ];/* large enough to hold string representation of an ipv4 / ipv6 address */ undo_type_t undo_type = (msg->family ==AF_INET6 ) ? undo_dns4 : undo_dns6;int addr_len = msg->addr_len;
@@ -1845,10 +1874,11 @@ msgptr->domains[_countof (msg->domains)-1 ] ='\0' ; } -wchar_t *wide_name =utf8to16 (msg->iface.name);/* utf8 to wide-char */ -if (!wide_name) +WCHAR iid[64 ]; + err =InterfaceIdString (msg->iface.name, iid,_countof (iid)); +if (err) { -return ERROR_OUTOFMEMORY ; +return err; }/* We delete all current addresses before adding any
@@ -1856,12 +1886,12 @@*/ if (addr_len >0 || msg->header.type == msg_del_dns_cfg) { - err =DeleteDNS (msg->family, wide_name); + err =ResetNameServers (iid, msg->family);if (err) { - goto out; +return err; } -free (RemoveListItem (&(*lists)[undo_type], CmpWString, wide_name)); +free (RemoveListItem (&(*lists)[undo_type], CmpAny, iid)); }if (msg->header.type == msg_del_dns_cfg)
@@ -1873,40 +1903,43 @@ err =SetDnsSearchDomains (msg->iface.name,NULL , &gpol, lists); }ApplyDnsSettings (gpol); - goto out;/* job done */ +return err;/* job done */ } -for (int i =0 ; i < addr_len; ++i) - { -if (msg->family ==AF_INET6 ) - { -RtlIpv6AddressToStringW (&msg->addr[i].ipv6, addr); - } -else - { -RtlIpv4AddressToStringW (&msg->addr[i].ipv4, addr); - } - err =AddDNS (msg->family, wide_name, addr); -if (i ==0 && err) - { - goto out; - } -/* We do not check for duplicate addresses, so any error in adding -* additional addresses is ignored. -*/ - } - - err =0 ; -if (msg->addr_len >0 ) { -wchar_t *tmp_name =_wcsdup (wide_name); -if (!tmp_name ||AddListItem (&(*lists)[undo_type], tmp_name)) +/* prepare the comma separated address list */ +CHAR addrs[256 ];/* large enough to hold four IPv4 / IPv6 address strings */ +size_t offset =0 ; +for (int i =0 ; i < addr_len; ++i) { -free (tmp_name); -DeleteDNS (msg->family, wide_name); - err =ERROR_OUTOFMEMORY ; - goto out; +if (i !=0 ) + { + addrs[offset++] =',' ; + } +if (msg->family ==AF_INET6 ) + { +RtlIpv6AddressToStringA (&msg->addr[i].ipv6, addrs + offset); + } +else + { +RtlIpv4AddressToStringA (&msg->addr[i].ipv4, addrs + offset); + } + offset +=strlen (addrs); + } + + err =SetNameServers (iid, msg->family, addrs); +if (err) + { +return err; + } + +wchar_t *tmp_iid =_wcsdup (iid); +if (!tmp_iid ||AddListItem (&(*lists)[undo_type], tmp_iid)) + { +free (tmp_iid); +ResetNameServers (iid, msg->family); +return ERROR_OUTOFMEMORY ; } }
@@ -1917,8 +1950,6 @@ }ApplyDnsSettings (gpol); -out: -free (wide_name);return err; }
@@ -2299,11 +2330,11 @@break ;case undo_dns4: -DeleteDNS (AF_INET , item->data); +ResetNameServers (item->data,AF_INET );break ;case undo_dns6: -DeleteDNS (AF_INET6 , item->data); +ResetNameServers (item->data,AF_INET6 );break ;case undo_domains:
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id2bed0908e84c19b8fb6fe806376316793e550b4
Gerrit-Change-Number: 825
Gerrit-PatchSet: 13
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-Reviewer: stipa <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-MessageType: newpatchset
PATCH v13 1/1 dns: do not use netsh to set name server addresses
In reply to a message not in the archiveFrom: Heiko Hund <[email protected]...>
Instead of spawning a netsh process, set the name server addresses directly in the registry hive of the VPN interface.
This is a first step to get rid of the use of command line tools in the service and move to a more API driven style of modifying the VPN adapter configuration.
---
This change was reviewed on Gerrit and approved by at least one developer. I request to merge it to master.
Gerrit URL: https://gerrit.openvpn.net/c/openvpn/+/825
This mail reflects revision 13 of this Change.
Acked-by according to Gerrit (reflected above):
Lev Stipakov <[email protected]...>
src/openvpnserv/interactive.c +146 -115
@@ -1024,65 +1024,6 @@ }/** -* Run the command: netsh interface $proto $action dns $if_name $addr [validate=no] -* @param action "delete" or "add" -* @param proto "ipv6" or "ip" -* @param if_name "name_of_interface" -* @param addr IPv4 (for proto = ip) or IPv6 address as a string -* -* If addr is null and action = "delete" all addresses are deleted. -*/ -static DWORD -netsh_dns_cmd (const wchar_t *action,const wchar_t *proto,const wchar_t *if_name,const wchar_t *addr) -{ -DWORD err =0 ; -int timeout =30000 ;/* in msec */ -wchar_t argv0[MAX_PATH ]; -wchar_t *cmdline =NULL ; - -if (!addr) - { -if (wcscmp (action,L"delete" ) ==0 ) - { - addr =L"all" ; - } -else /* nothing to do -- return success*/ - { - goto out; - } - } - -/* Path of netsh */ -swprintf (argv0,_countof (argv0),L"%ls\\%ls" ,get_win_sys_path (),L"netsh.exe" ); - -/* cmd template: -* netsh interface $proto $action dns $if_name $addr [validate=no] -*/ -const wchar_t *fmt =L"netsh interface %ls %ls dns \"%ls\" %ls" ; - -/* max cmdline length in wchars -- include room for worst case and some */ -size_t ncmdline =wcslen (fmt) +wcslen (if_name) +wcslen (addr) +32 +1 ; - cmdline =malloc (ncmdline*sizeof (wchar_t)); -if (!cmdline) - { - err =ERROR_OUTOFMEMORY ; - goto out; - } - -swprintf (cmdline, ncmdline, fmt, proto, action, if_name, addr); - -if (IsWindows7OrGreater ()) - { -wcscat_s (cmdline, ncmdline,L" validate=no" ); - } - err =ExecCommand (argv0, cmdline, timeout); - -out: -free (cmdline); -return err; -} - -/** * Run the command: netsh interface ip $action wins $if_name [static] $addr * @param action "delete", "add" or "set" * @param if_name "name_of_interface"
@@ -1139,22 +1080,6 @@return err; } -/* Delete all IPv4 or IPv6 dns servers for an interface */ -static DWORD -DeleteDNS (short family,wchar_t *if_name) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"delete" , proto, if_name,NULL ); -} - -/* Add an IPv4 or IPv6 dns server to an interface */ -static DWORD -AddDNS (short family,wchar_t *if_name,wchar_t *addr) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"add" , proto, if_name, addr); -} -static BOOL CmpWString (LPVOID item,LPVOID str) {
@@ -1819,11 +1744,115 @@return err; } +/** +* Return the interfaces registry key for the specified address family +* +* @param family the internet address family to open the key for +* @param key PHKEY to return the key in +* @return BOOL to indicate success or failure +*/ +static BOOL +GetInterfacesKey (short family,PHKEY key) +{ +PCSTR itfs_key = family ==AF_INET6 + ?"SYSTEM\\CurrentControlSet\\Services\\Tcpip6\\Parameters\\Interfaces" + :"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces" ; + +LSTATUS err =RegOpenKeyExA (HKEY_LOCAL_MACHINE , itfs_key,0 ,KEY_ALL_ACCESS , key); +if (err) + { + *key =INVALID_HANDLE_VALUE ; +MsgToEventLog (M_SYSERR ,TEXT ("GetInterfacesKey: " +"could not open interfaces registry key for family %d (%lu)" ), + family, err); + } + +return err ? FALSE : TRUE; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param value the value to set the name servers to +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServersValue (PCWSTR itf_id,short family,PCSTR value) +{ +DWORD err; + +HKEY itfs; +if (!GetInterfacesKey (family, &itfs)) + { +return ERROR_FILE_NOT_FOUND ; + } + +HKEY itf =INVALID_HANDLE_VALUE ; + err =RegOpenKeyExW (itfs, itf_id,0 ,KEY_ALL_ACCESS , &itf); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not open interface key for %s family %d (%lu)" ), + itf_id, family, err); + goto out; + } + + err =RegSetValueExA (itf,"NameServer" ,0 ,REG_SZ , (PBYTE )value,strlen (value) +1 ); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not set name servers '%S' for %s family %d (%lu)" ), + value, itf_id, family, err); + } + +out: +if (itf !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itf); + } +if (itfs !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itfs); + } +return err; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param addrs comma separated list of name server addresses +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServers (PCWSTR itf_id,short family,PCSTR addrs) +{ +return SetNameServersValue (itf_id, family, addrs); +} + +/** +* Delete all DNS name servers from a registry interface configuration +* +* @param itf_id the interface id to clear the servers for +* @param family internet address family to clear the servers for +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +ResetNameServers (PCWSTR itf_id,short family) +{ +return SetNameServersValue (itf_id, family,"" ); +} +static DWORD HandleDNSConfigMessage (const dns_cfg_message_t *msg,undo_lists_t *lists) {DWORD err =0 ; -wchar_t addr[46 ];/* large enough to hold string representation of an ipv4 / ipv6 address */ undo_type_t undo_type = (msg->family ==AF_INET6 ) ? undo_dns4 : undo_dns6;int addr_len = msg->addr_len;
@@ -1845,10 +1874,11 @@ msgptr->domains[_countof (msg->domains)-1 ] ='\0' ; } -wchar_t *wide_name =utf8to16 (msg->iface.name);/* utf8 to wide-char */ -if (!wide_name) +WCHAR iid[64 ]; + err =InterfaceIdString (msg->iface.name, iid,_countof (iid)); +if (err) { -return ERROR_OUTOFMEMORY ; +return err; }/* We delete all current addresses before adding any
@@ -1856,12 +1886,12 @@*/ if (addr_len >0 || msg->header.type == msg_del_dns_cfg) { - err =DeleteDNS (msg->family, wide_name); + err =ResetNameServers (iid, msg->family);if (err) { - goto out; +return err; } -free (RemoveListItem (&(*lists)[undo_type], CmpWString, wide_name)); +free (RemoveListItem (&(*lists)[undo_type], CmpAny, iid)); }if (msg->header.type == msg_del_dns_cfg)
@@ -1873,40 +1903,43 @@ err =SetDnsSearchDomains (msg->iface.name,NULL , &gpol, lists); }ApplyDnsSettings (gpol); - goto out;/* job done */ +return err;/* job done */ } -for (int i =0 ; i < addr_len; ++i) - { -if (msg->family ==AF_INET6 ) - { -RtlIpv6AddressToStringW (&msg->addr[i].ipv6, addr); - } -else - { -RtlIpv4AddressToStringW (&msg->addr[i].ipv4, addr); - } - err =AddDNS (msg->family, wide_name, addr); -if (i ==0 && err) - { - goto out; - } -/* We do not check for duplicate addresses, so any error in adding -* additional addresses is ignored. -*/ - } - - err =0 ; -if (msg->addr_len >0 ) { -wchar_t *tmp_name =_wcsdup (wide_name); -if (!tmp_name ||AddListItem (&(*lists)[undo_type], tmp_name)) +/* prepare the comma separated address list */ +CHAR addrs[256 ];/* large enough to hold four IPv4 / IPv6 address strings */ +size_t offset =0 ; +for (int i =0 ; i < addr_len; ++i) { -free (tmp_name); -DeleteDNS (msg->family, wide_name); - err =ERROR_OUTOFMEMORY ; - goto out; +if (i !=0 ) + { + addrs[offset++] =',' ; + } +if (msg->family ==AF_INET6 ) + { +RtlIpv6AddressToStringA (&msg->addr[i].ipv6, addrs + offset); + } +else + { +RtlIpv4AddressToStringA (&msg->addr[i].ipv4, addrs + offset); + } + offset +=strlen (addrs); + } + + err =SetNameServers (iid, msg->family, addrs); +if (err) + { +return err; + } + +wchar_t *tmp_iid =_wcsdup (iid); +if (!tmp_iid ||AddListItem (&(*lists)[undo_type], tmp_iid)) + { +free (tmp_iid); +ResetNameServers (iid, msg->family); +return ERROR_OUTOFMEMORY ; } }
@@ -1917,8 +1950,6 @@ }ApplyDnsSettings (gpol); -out: -free (wide_name);return err; }
@@ -2299,11 +2330,11 @@break ;case undo_dns4: -DeleteDNS (AF_INET , item->data); +ResetNameServers (item->data,AF_INET );break ;case undo_dns6: -DeleteDNS (AF_INET6 , item->data); +ResetNameServers (item->data,AF_INET6 );break ;case undo_domains:
[Openvpn-devel] [PATCH applied] Re: dns: do not use netsh to set name server addresses
Stared at code, looks reasonable.
There is a bit of unsafe code (the address copying loop will overrun *if* we ever increase the number of allowed addresses in dns_cfg_message_t and forget to increase this buffer) - but it cannot overflow with anything accepted today, and increasing the message size is not something trivially done.
Lev has tested that it actually works :-)
Since mail-archive.org is broken again I've referenced the sf.net mail archive (+ the gerrit URL) instead.
Your patch has been applied to the master branch.
commit 99b35c3783477b837735cff0aa1cf7c5ba4aa366
Author: Heiko Hund Date: Wed Mar 12 11:11:50 2025 +0100
dns: do not use netsh to set name server addresses
Signed-off-by: Heiko Hund <[email protected]...>
Acked-by: Lev Stipakov <[email protected]...>
Message-Id: <[email protected]...>
URL: https://sourceforge.net/p/openvpn/mailman/message/59159531/
URL: https://gerrit.openvpn.net/c/openvpn/+/825
Signed-off-by: Gert Doering <[email protected]...>--
kind regards,
Gert Doering
Attention is currently required from: d12fk, flichtenheld, plaisthos.
cron2 has posted comments on this change. ( http://gerrit.openvpn.net/c/openvpn/+/825?usp=email )
Change subject: dns: do not use netsh to set name server addresses
......................................................................
Patch Set 13:
(1 comment)
File src/openvpnserv/interactive.c:
http://gerrit.openvpn.net/c/openvpn/+/825/comment/34e70988_8404e3c3 :
PS13, Line 1929: }
I am a bit worried about this static buffer and no overflow check - if we ever increase the dns_cfg_message_t to addr[8], will we remember to also increase the CHAR addrs[256] here?
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id2bed0908e84c19b8fb6fe806376316793e550b4
Gerrit-Change-Number: 825
Gerrit-PatchSet: 13
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-Reviewer: stipa <[email protected]...>
Gerrit-CC: cron2 <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-Attention: plaisthos <[email protected]...>
Gerrit-Attention: flichtenheld <[email protected]...>
Gerrit-Attention: d12fk <[email protected]...>
Gerrit-Comment-Date: Wed, 12 Mar 2025 10:54:44 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment
cron2 has uploaded a new patch set (#14) to the change originally created by d12fk. ( http://gerrit.openvpn.net/c/openvpn/+/825?usp=email )
The following approvals got outdated and were removed:
Code-Review+2 by stipa
Change subject: dns: do not use netsh to set name server addresses
......................................................................
dns: do not use netsh to set name server addresses
Instead of spawning a netsh process, set the name server addresses directly in the registry hive of the VPN interface.
This is a first step to get rid of the use of command line tools in the service and move to a more API driven style of modifying the VPN adapter configuration.
Message-Id: <[email protected]...>
URL: https://sourceforge.net/p/openvpn/mailman/message/59159531/
URL: https://gerrit.openvpn.net/c/openvpn/+/825
---
M src/openvpnserv/interactive.c
1 file changed, 146 insertions(+), 115 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/25/825/14
src/openvpnserv/interactive.c +146 -115
@@ -1024,65 +1024,6 @@ }/** -* Run the command: netsh interface $proto $action dns $if_name $addr [validate=no] -* @param action "delete" or "add" -* @param proto "ipv6" or "ip" -* @param if_name "name_of_interface" -* @param addr IPv4 (for proto = ip) or IPv6 address as a string -* -* If addr is null and action = "delete" all addresses are deleted. -*/ -static DWORD -netsh_dns_cmd (const wchar_t *action,const wchar_t *proto,const wchar_t *if_name,const wchar_t *addr) -{ -DWORD err =0 ; -int timeout =30000 ;/* in msec */ -wchar_t argv0[MAX_PATH ]; -wchar_t *cmdline =NULL ; - -if (!addr) - { -if (wcscmp (action,L"delete" ) ==0 ) - { - addr =L"all" ; - } -else /* nothing to do -- return success*/ - { - goto out; - } - } - -/* Path of netsh */ -swprintf (argv0,_countof (argv0),L"%ls\\%ls" ,get_win_sys_path (),L"netsh.exe" ); - -/* cmd template: -* netsh interface $proto $action dns $if_name $addr [validate=no] -*/ -const wchar_t *fmt =L"netsh interface %ls %ls dns \"%ls\" %ls" ; - -/* max cmdline length in wchars -- include room for worst case and some */ -size_t ncmdline =wcslen (fmt) +wcslen (if_name) +wcslen (addr) +32 +1 ; - cmdline =malloc (ncmdline*sizeof (wchar_t)); -if (!cmdline) - { - err =ERROR_OUTOFMEMORY ; - goto out; - } - -swprintf (cmdline, ncmdline, fmt, proto, action, if_name, addr); - -if (IsWindows7OrGreater ()) - { -wcscat_s (cmdline, ncmdline,L" validate=no" ); - } - err =ExecCommand (argv0, cmdline, timeout); - -out: -free (cmdline); -return err; -} - -/** * Run the command: netsh interface ip $action wins $if_name [static] $addr * @param action "delete", "add" or "set" * @param if_name "name_of_interface"
@@ -1139,22 +1080,6 @@return err; } -/* Delete all IPv4 or IPv6 dns servers for an interface */ -static DWORD -DeleteDNS (short family,wchar_t *if_name) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"delete" , proto, if_name,NULL ); -} - -/* Add an IPv4 or IPv6 dns server to an interface */ -static DWORD -AddDNS (short family,wchar_t *if_name,wchar_t *addr) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"add" , proto, if_name, addr); -} -static BOOL CmpWString (LPVOID item,LPVOID str) {
@@ -1819,11 +1744,115 @@return err; } +/** +* Return the interfaces registry key for the specified address family +* +* @param family the internet address family to open the key for +* @param key PHKEY to return the key in +* @return BOOL to indicate success or failure +*/ +static BOOL +GetInterfacesKey (short family,PHKEY key) +{ +PCSTR itfs_key = family ==AF_INET6 + ?"SYSTEM\\CurrentControlSet\\Services\\Tcpip6\\Parameters\\Interfaces" + :"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces" ; + +LSTATUS err =RegOpenKeyExA (HKEY_LOCAL_MACHINE , itfs_key,0 ,KEY_ALL_ACCESS , key); +if (err) + { + *key =INVALID_HANDLE_VALUE ; +MsgToEventLog (M_SYSERR ,TEXT ("GetInterfacesKey: " +"could not open interfaces registry key for family %d (%lu)" ), + family, err); + } + +return err ? FALSE : TRUE; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param value the value to set the name servers to +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServersValue (PCWSTR itf_id,short family,PCSTR value) +{ +DWORD err; + +HKEY itfs; +if (!GetInterfacesKey (family, &itfs)) + { +return ERROR_FILE_NOT_FOUND ; + } + +HKEY itf =INVALID_HANDLE_VALUE ; + err =RegOpenKeyExW (itfs, itf_id,0 ,KEY_ALL_ACCESS , &itf); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not open interface key for %s family %d (%lu)" ), + itf_id, family, err); + goto out; + } + + err =RegSetValueExA (itf,"NameServer" ,0 ,REG_SZ , (PBYTE )value,strlen (value) +1 ); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not set name servers '%S' for %s family %d (%lu)" ), + value, itf_id, family, err); + } + +out: +if (itf !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itf); + } +if (itfs !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itfs); + } +return err; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param addrs comma separated list of name server addresses +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServers (PCWSTR itf_id,short family,PCSTR addrs) +{ +return SetNameServersValue (itf_id, family, addrs); +} + +/** +* Delete all DNS name servers from a registry interface configuration +* +* @param itf_id the interface id to clear the servers for +* @param family internet address family to clear the servers for +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +ResetNameServers (PCWSTR itf_id,short family) +{ +return SetNameServersValue (itf_id, family,"" ); +} +static DWORD HandleDNSConfigMessage (const dns_cfg_message_t *msg,undo_lists_t *lists) {DWORD err =0 ; -wchar_t addr[46 ];/* large enough to hold string representation of an ipv4 / ipv6 address */ undo_type_t undo_type = (msg->family ==AF_INET6 ) ? undo_dns4 : undo_dns6;int addr_len = msg->addr_len;
@@ -1845,10 +1874,11 @@ msgptr->domains[_countof (msg->domains)-1 ] ='\0' ; } -wchar_t *wide_name =utf8to16 (msg->iface.name);/* utf8 to wide-char */ -if (!wide_name) +WCHAR iid[64 ]; + err =InterfaceIdString (msg->iface.name, iid,_countof (iid)); +if (err) { -return ERROR_OUTOFMEMORY ; +return err; }/* We delete all current addresses before adding any
@@ -1856,12 +1886,12 @@*/ if (addr_len >0 || msg->header.type == msg_del_dns_cfg) { - err =DeleteDNS (msg->family, wide_name); + err =ResetNameServers (iid, msg->family);if (err) { - goto out; +return err; } -free (RemoveListItem (&(*lists)[undo_type], CmpWString, wide_name)); +free (RemoveListItem (&(*lists)[undo_type], CmpAny, iid)); }if (msg->header.type == msg_del_dns_cfg)
@@ -1873,40 +1903,43 @@ err =SetDnsSearchDomains (msg->iface.name,NULL , &gpol, lists); }ApplyDnsSettings (gpol); - goto out;/* job done */ +return err;/* job done */ } -for (int i =0 ; i < addr_len; ++i) - { -if (msg->family ==AF_INET6 ) - { -RtlIpv6AddressToStringW (&msg->addr[i].ipv6, addr); - } -else - { -RtlIpv4AddressToStringW (&msg->addr[i].ipv4, addr); - } - err =AddDNS (msg->family, wide_name, addr); -if (i ==0 && err) - { - goto out; - } -/* We do not check for duplicate addresses, so any error in adding -* additional addresses is ignored. -*/ - } - - err =0 ; -if (msg->addr_len >0 ) { -wchar_t *tmp_name =_wcsdup (wide_name); -if (!tmp_name ||AddListItem (&(*lists)[undo_type], tmp_name)) +/* prepare the comma separated address list */ +CHAR addrs[256 ];/* large enough to hold four IPv4 / IPv6 address strings */ +size_t offset =0 ; +for (int i =0 ; i < addr_len; ++i) { -free (tmp_name); -DeleteDNS (msg->family, wide_name); - err =ERROR_OUTOFMEMORY ; - goto out; +if (i !=0 ) + { + addrs[offset++] =',' ; + } +if (msg->family ==AF_INET6 ) + { +RtlIpv6AddressToStringA (&msg->addr[i].ipv6, addrs + offset); + } +else + { +RtlIpv4AddressToStringA (&msg->addr[i].ipv4, addrs + offset); + } + offset +=strlen (addrs); + } + + err =SetNameServers (iid, msg->family, addrs); +if (err) + { +return err; + } + +wchar_t *tmp_iid =_wcsdup (iid); +if (!tmp_iid ||AddListItem (&(*lists)[undo_type], tmp_iid)) + { +free (tmp_iid); +ResetNameServers (iid, msg->family); +return ERROR_OUTOFMEMORY ; } }
@@ -1917,8 +1950,6 @@ }ApplyDnsSettings (gpol); -out: -free (wide_name);return err; }
@@ -2299,11 +2330,11 @@break ;case undo_dns4: -DeleteDNS (AF_INET , item->data); +ResetNameServers (item->data,AF_INET );break ;case undo_dns6: -DeleteDNS (AF_INET6 , item->data); +ResetNameServers (item->data,AF_INET6 );break ;case undo_domains:
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id2bed0908e84c19b8fb6fe806376316793e550b4
Gerrit-Change-Number: 825
Gerrit-PatchSet: 14
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-Reviewer: stipa <[email protected]...>
Gerrit-CC: cron2 <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-MessageType: newpatchset
cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/825?usp=email )
Change subject: dns: do not use netsh to set name server addresses
......................................................................
dns: do not use netsh to set name server addresses
Instead of spawning a netsh process, set the name server addresses directly in the registry hive of the VPN interface.
This is a first step to get rid of the use of command line tools in the service and move to a more API driven style of modifying the VPN adapter configuration.
Message-Id: <[email protected]...>
URL: https://sourceforge.net/p/openvpn/mailman/message/59159531/
URL: https://gerrit.openvpn.net/c/openvpn/+/825
---
M src/openvpnserv/interactive.c
1 file changed, 146 insertions(+), 115 deletions(-)
src/openvpnserv/interactive.c +146 -115
@@ -1024,65 +1024,6 @@ }/** -* Run the command: netsh interface $proto $action dns $if_name $addr [validate=no] -* @param action "delete" or "add" -* @param proto "ipv6" or "ip" -* @param if_name "name_of_interface" -* @param addr IPv4 (for proto = ip) or IPv6 address as a string -* -* If addr is null and action = "delete" all addresses are deleted. -*/ -static DWORD -netsh_dns_cmd (const wchar_t *action,const wchar_t *proto,const wchar_t *if_name,const wchar_t *addr) -{ -DWORD err =0 ; -int timeout =30000 ;/* in msec */ -wchar_t argv0[MAX_PATH ]; -wchar_t *cmdline =NULL ; - -if (!addr) - { -if (wcscmp (action,L"delete" ) ==0 ) - { - addr =L"all" ; - } -else /* nothing to do -- return success*/ - { - goto out; - } - } - -/* Path of netsh */ -swprintf (argv0,_countof (argv0),L"%ls\\%ls" ,get_win_sys_path (),L"netsh.exe" ); - -/* cmd template: -* netsh interface $proto $action dns $if_name $addr [validate=no] -*/ -const wchar_t *fmt =L"netsh interface %ls %ls dns \"%ls\" %ls" ; - -/* max cmdline length in wchars -- include room for worst case and some */ -size_t ncmdline =wcslen (fmt) +wcslen (if_name) +wcslen (addr) +32 +1 ; - cmdline =malloc (ncmdline*sizeof (wchar_t)); -if (!cmdline) - { - err =ERROR_OUTOFMEMORY ; - goto out; - } - -swprintf (cmdline, ncmdline, fmt, proto, action, if_name, addr); - -if (IsWindows7OrGreater ()) - { -wcscat_s (cmdline, ncmdline,L" validate=no" ); - } - err =ExecCommand (argv0, cmdline, timeout); - -out: -free (cmdline); -return err; -} - -/** * Run the command: netsh interface ip $action wins $if_name [static] $addr * @param action "delete", "add" or "set" * @param if_name "name_of_interface"
@@ -1139,22 +1080,6 @@return err; } -/* Delete all IPv4 or IPv6 dns servers for an interface */ -static DWORD -DeleteDNS (short family,wchar_t *if_name) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"delete" , proto, if_name,NULL ); -} - -/* Add an IPv4 or IPv6 dns server to an interface */ -static DWORD -AddDNS (short family,wchar_t *if_name,wchar_t *addr) -{ -wchar_t *proto = (family ==AF_INET6 ) ?L"ipv6" :L"ip" ; -return netsh_dns_cmd (L"add" , proto, if_name, addr); -} -static BOOL CmpWString (LPVOID item,LPVOID str) {
@@ -1819,11 +1744,115 @@return err; } +/** +* Return the interfaces registry key for the specified address family +* +* @param family the internet address family to open the key for +* @param key PHKEY to return the key in +* @return BOOL to indicate success or failure +*/ +static BOOL +GetInterfacesKey (short family,PHKEY key) +{ +PCSTR itfs_key = family ==AF_INET6 + ?"SYSTEM\\CurrentControlSet\\Services\\Tcpip6\\Parameters\\Interfaces" + :"SYSTEM\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces" ; + +LSTATUS err =RegOpenKeyExA (HKEY_LOCAL_MACHINE , itfs_key,0 ,KEY_ALL_ACCESS , key); +if (err) + { + *key =INVALID_HANDLE_VALUE ; +MsgToEventLog (M_SYSERR ,TEXT ("GetInterfacesKey: " +"could not open interfaces registry key for family %d (%lu)" ), + family, err); + } + +return err ? FALSE : TRUE; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param value the value to set the name servers to +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServersValue (PCWSTR itf_id,short family,PCSTR value) +{ +DWORD err; + +HKEY itfs; +if (!GetInterfacesKey (family, &itfs)) + { +return ERROR_FILE_NOT_FOUND ; + } + +HKEY itf =INVALID_HANDLE_VALUE ; + err =RegOpenKeyExW (itfs, itf_id,0 ,KEY_ALL_ACCESS , &itf); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not open interface key for %s family %d (%lu)" ), + itf_id, family, err); + goto out; + } + + err =RegSetValueExA (itf,"NameServer" ,0 ,REG_SZ , (PBYTE )value,strlen (value) +1 ); +if (err) + { +MsgToEventLog (M_SYSERR ,TEXT ("SetNameServersValue: " +"could not set name servers '%S' for %s family %d (%lu)" ), + value, itf_id, family, err); + } + +out: +if (itf !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itf); + } +if (itfs !=INVALID_HANDLE_VALUE ) + { +RegCloseKey (itfs); + } +return err; +} + +/** +* Set the DNS name servers in a registry interface configuration +* +* @param itf_id the interface id to set the servers for +* @param family internet address family to set the servers for +* @param addrs comma separated list of name server addresses +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +SetNameServers (PCWSTR itf_id,short family,PCSTR addrs) +{ +return SetNameServersValue (itf_id, family, addrs); +} + +/** +* Delete all DNS name servers from a registry interface configuration +* +* @param itf_id the interface id to clear the servers for +* @param family internet address family to clear the servers for +* +* @return DWORD NO_ERROR on success, a Windows error code otherwise +*/ +static DWORD +ResetNameServers (PCWSTR itf_id,short family) +{ +return SetNameServersValue (itf_id, family,"" ); +} +static DWORD HandleDNSConfigMessage (const dns_cfg_message_t *msg,undo_lists_t *lists) {DWORD err =0 ; -wchar_t addr[46 ];/* large enough to hold string representation of an ipv4 / ipv6 address */ undo_type_t undo_type = (msg->family ==AF_INET6 ) ? undo_dns4 : undo_dns6;int addr_len = msg->addr_len;
@@ -1845,10 +1874,11 @@ msgptr->domains[_countof (msg->domains)-1 ] ='\0' ; } -wchar_t *wide_name =utf8to16 (msg->iface.name);/* utf8 to wide-char */ -if (!wide_name) +WCHAR iid[64 ]; + err =InterfaceIdString (msg->iface.name, iid,_countof (iid)); +if (err) { -return ERROR_OUTOFMEMORY ; +return err; }/* We delete all current addresses before adding any
@@ -1856,12 +1886,12 @@*/ if (addr_len >0 || msg->header.type == msg_del_dns_cfg) { - err =DeleteDNS (msg->family, wide_name); + err =ResetNameServers (iid, msg->family);if (err) { - goto out; +return err; } -free (RemoveListItem (&(*lists)[undo_type], CmpWString, wide_name)); +free (RemoveListItem (&(*lists)[undo_type], CmpAny, iid)); }if (msg->header.type == msg_del_dns_cfg)
@@ -1873,40 +1903,43 @@ err =SetDnsSearchDomains (msg->iface.name,NULL , &gpol, lists); }ApplyDnsSettings (gpol); - goto out;/* job done */ +return err;/* job done */ } -for (int i =0 ; i < addr_len; ++i) - { -if (msg->family ==AF_INET6 ) - { -RtlIpv6AddressToStringW (&msg->addr[i].ipv6, addr); - } -else - { -RtlIpv4AddressToStringW (&msg->addr[i].ipv4, addr); - } - err =AddDNS (msg->family, wide_name, addr); -if (i ==0 && err) - { - goto out; - } -/* We do not check for duplicate addresses, so any error in adding -* additional addresses is ignored. -*/ - } - - err =0 ; -if (msg->addr_len >0 ) { -wchar_t *tmp_name =_wcsdup (wide_name); -if (!tmp_name ||AddListItem (&(*lists)[undo_type], tmp_name)) +/* prepare the comma separated address list */ +CHAR addrs[256 ];/* large enough to hold four IPv4 / IPv6 address strings */ +size_t offset =0 ; +for (int i =0 ; i < addr_len; ++i) { -free (tmp_name); -DeleteDNS (msg->family, wide_name); - err =ERROR_OUTOFMEMORY ; - goto out; +if (i !=0 ) + { + addrs[offset++] =',' ; + } +if (msg->family ==AF_INET6 ) + { +RtlIpv6AddressToStringA (&msg->addr[i].ipv6, addrs + offset); + } +else + { +RtlIpv4AddressToStringA (&msg->addr[i].ipv4, addrs + offset); + } + offset +=strlen (addrs); + } + + err =SetNameServers (iid, msg->family, addrs); +if (err) + { +return err; + } + +wchar_t *tmp_iid =_wcsdup (iid); +if (!tmp_iid ||AddListItem (&(*lists)[undo_type], tmp_iid)) + { +free (tmp_iid); +ResetNameServers (iid, msg->family); +return ERROR_OUTOFMEMORY ; } }
@@ -1917,8 +1950,6 @@ }ApplyDnsSettings (gpol); -out: -free (wide_name);return err; }
@@ -2299,11 +2330,11 @@break ;case undo_dns4: -DeleteDNS (AF_INET , item->data); +ResetNameServers (item->data,AF_INET );break ;case undo_dns6: -DeleteDNS (AF_INET6 , item->data); +ResetNameServers (item->data,AF_INET6 );break ;case undo_domains:
--
To view, visit http://gerrit.openvpn.net/c/openvpn/+/825?usp=email
To unsubscribe, or for help writing mail filters, visit http://gerrit.openvpn.net/settings
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id2bed0908e84c19b8fb6fe806376316793e550b4
Gerrit-Change-Number: 825
Gerrit-PatchSet: 14
Gerrit-Owner: d12fk <[email protected]...>
Gerrit-Reviewer: flichtenheld <[email protected]...>
Gerrit-Reviewer: plaisthos <[email protected]...>
Gerrit-Reviewer: stipa <[email protected]...>
Gerrit-CC: cron2 <[email protected]...>
Gerrit-CC: openvpn-devel <[email protected]>
Gerrit-MessageType: merged